上篇文章需要读取当前java或者配置文件的编码格式,这里主要支持UTF-8、GBK、UTF-16、Unicode等

/**
* 判断文件的编码格式
* @param fileName :file
* @return 文件编码格式
* @throws Exception
*/
public static String codeString(File fileName) throws Exception{
BufferedInputStream bin = new BufferedInputStream(
new FileInputStream(fileName));
int p = (bin.read() << 8) + bin.read();
String code = null; switch (p) {
case 0xefbb:
code = "UTF-8";
break;
case 0xfffe:
code = "Unicode";
break;
case 0xfeff:
code = "UTF-16BE";
break;
default:
code = "GBK";
}
IOUtils.closeQuietly(bin);
return code;
}

上面这段代码只能判断带bom的文本,如果非bom文本,还有两种方式
1、 轮询常用的编码,知道找到匹配的,如下面一段测试代码

  

/*
* Copyright 2010 Georgios Migdos <cyberpython@gmail.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/ import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder; /**
*
* @author Georgios Migdos <cyberpython@gmail.com>
*/
public class CharsetDetector { public Charset detectCharset(File f, String[] charsets) { Charset charset = null; for (String charsetName : charsets) {
charset = detectCharset(f, Charset.forName(charsetName));
if (charset != null) {
break;
}
} return charset;
} private Charset detectCharset(File f, Charset charset) {
try {
BufferedInputStream input = new BufferedInputStream(new FileInputStream(f)); CharsetDecoder decoder = charset.newDecoder();
decoder.reset(); byte[] buffer = new byte[512];
boolean identified = false;
while ((input.read(buffer) != -1) && (!identified)) {
identified = identify(buffer, decoder);
} input.close(); if (identified) {
return charset;
} else {
return null;
} } catch (Exception e) {
return null;
}
} private boolean identify(byte[] bytes, CharsetDecoder decoder) {
try {
decoder.decode(ByteBuffer.wrap(bytes));
} catch (CharacterCodingException e) {
return false;
}
return true;
} public static void main(String[] args) {
File f = new File("example.txt"); String[] charsetsToBeTested = {"UTF-8", "windows-1253", "ISO-8859-7"}; CharsetDetector cd = new CharsetDetector();
Charset charset = cd.detectCharset(f, charsetsToBeTested); if (charset != null) {
try {
InputStreamReader reader = new InputStreamReader(new FileInputStream(f), charset);
int c = 0;
while ((c = reader.read()) != -1) {
System.out.print((char)c);
}
reader.close();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
} }else{
System.out.println("Unrecognized charset.");
}
}
}

2、 使用谷歌依赖库来进行判断
https://code.google.com/archive/p/juniversalchardet/

java判断文本文件编码格式的更多相关文章

  1. Java判断文件编码格式

    转自:http://blog.csdn.net/zhangzh332/article/details/6719025 一般情况下我们遇到的文件编码格式为GBK或者UTF-8.由于中文Windows默认 ...

  2. Java推断文本文件编码格式以及读取

    假设不是约定好的,要想解析txt文件就须要知道文件编码类型,因为文件编码类型众多.比如UTF-8,GBK.UTF-16,GB2312等等. 事实上有简单的办法.仅仅须要这样就能够了 String fi ...

  3. (转) Java读取文本文件中文乱码问题

    http://blog.csdn.net/greenqingqingws/article/details/7395213 最近遇到一个问题,Java读取文本文件(例如csv文件.txt文件等),遇到中 ...

  4. Java读取文本文件中文乱码问题 .转载

    最近遇到一个问题,Java读取文本文件(例如csv文件.txt文件等),遇到中文就变成乱码.读取代码如下: List<String> lines=new ArrayList<Stri ...

  5. Eclipse: eclipse文本文件编码格式更改(GBK——UTF-8)

    Eclipse中设置编码的方式 Eclipse工 作空间(workspace)的缺省字符编码是操作系统缺省的编码,简体中文操作系统 (Windows XP.Windows 2000简体中文)的缺省编码 ...

  6. Java读取文本文件中文乱码问题

    最近遇到一个问题,Java读取文本文件(例如csv文件.txt文件等),遇到中文就变成乱码.读取代码如下: List<String> lines=new ArrayList<Stri ...

  7. Java读写文本文件操作

    package com.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; ...

  8. 使用Java判断字符串中的中文字符数量

    Java判断一个字符串str中中文的个数,经过总结,有以下几种方法(全部经过验证),可根据其原理判断在何种情况下使用哪个方法: 1. char[] c = str.toCharArray(); for ...

  9. Java判断回文数算法简单实现

    好久没写java的代码了, 今天闲来无事写段java的代码,算是为新的一年磨磨刀,开个头,算法是Java判断回文数算法简单实现,基本思想是利用字符串对应位置比较,如果所有可能位置都满足要求,则输入的是 ...

  10. Java 判断操作系统类型(适用于各种操作系统)

    Java 判断操作系统类型(适用于各种操作系统) 最近一段时间写一个授权的程序,需要获取很多信息来保证程序不能随意复制使用,必须经过授权才可以. 为了限制用户使用的操作系统,必须有统一的方法来获取才可 ...

随机推荐

  1. 一个基于SSM的CRUD的标准写法

    CRUD即CREATE,READ,UPDATE,DELETE的首字母的合写,意思是增读改删.前人为了便于发音和理解,改为增删改查. CRUD基本上是软件开发中中相当部分功能的最小功能模块构成,虽然软件 ...

  2. 深入探索 Nuxt3 Composables:掌握目录架构与内置API的高效应用

    title: 深入探索 Nuxt3 Composables:掌握目录架构与内置API的高效应用 date: 2024/6/23 updated: 2024/6/23 author: cmdragon ...

  3. .NET Core 中生成验证码

    在开发中,有时候生成验证码的场景目前还是存在的,本篇演示不依赖第三方组件,生成随机验证码图片. 先添加验证码接口 public interface ICaptcha { /// <summary ...

  4. Java 对象转XML xStream 别名的使用 附下载方式

    下载方式 Maven方式 pom.xml中 <dependency> <groupId>xstream</groupId> <artifactId>xs ...

  5. springboot异常解决

    问题解决 问题解释 出现这个问题表示拦截器或控制器的某个请求处理方法返回了一个与请求路径相同的视图名称,导致视图解析器循环地尝试解析并渲染这个视图,从而引发循环视图路径的异常. 问题分析 原先的jav ...

  6. SQL注入漏洞攻击

    l-> 对于用户登录的实现,提供SQL语句 •-> select * from 表名 where uid=- and pwd=- •-> 使用字符串拼接 l-> 提供密码为:' ...

  7. Vscode控制台乱码的最终解决方案

    Vscode控制台乱码的最终解决方案 vscode运行项目时控制台打印日志乱码.网上也有许多解决办法. 方法一[管用]推荐,避免过多设置 Java项目时,像Springboot微服务项目默认使用的是l ...

  8. 解决方案 | 预装win11如何退回win10?

    0.定义 本文所说的[退回]并不指的是win10升级后的变成win11再变为win10的退回.退回应该理解为[降级],或者叫作返回上一个版本.本文的适用范围局限于,预装系统是win11,想要不通过u盘 ...

  9. LLM-01 大模型 本地部署运行 ChatGLM2-6B-INT4(6GB) 简单上手 环境配置 单机单卡多卡 2070Super8GBx2 打怪升级!

    搬迁说明 之前在 CSDN 上发文章,一直想着努力发一些好的文章出来!这篇文章在 2024-04-17 10:11:55 已在 CSDN 发布 写在前面 其他显卡环境也可以!但是最少要有8GB的显存, ...

  10. RHCA cl210 015 实例启动 超融合 热迁移 网络underlay

    lab computeresources-hci setup 实例启动流程 keystone不仅做认证,且有所有组键地址 nova-conductor解耦,不允许nova-compute直接访问dat ...