java自动探测文件的字符编码
Mozilla有一个C++版的自动字符集探测算法代码,然后sourceforge上有人将其改成java版的~~
主页:http://jchardet.sourceforge.net/
jchardet is a java port of the source from mozilla's automatic charset detection algorithm.
The original author is Frank Tang. What is available here is the java port of that code.
The original source in C++ can be found from http://lxr.mozilla.org/mozilla/source/intl/chardet/
More information can be found at http://www.mozilla.org/projects/intl/chardet.html
下面是见证奇迹的时刻:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import org.mozilla.intl.chardet.nsDetector;
import org.mozilla.intl.chardet.nsICharsetDetectionObserver; public class FileCharsetDetector {
private boolean found = false;
private String encoding = null; public static void main(String[] argv) throws Exception {
File file1 = new File("C:\\test1.txt"); System.out.println("文件编码:" + new FileCharsetDetector().guessFileEncoding(file1));
} /**
* 传入一个文件(File)对象,检查文件编码
*
* @param file
* File对象实例
* @return 文件编码,若无,则返回null
* @throws FileNotFoundException
* @throws IOException
*/
public String guessFileEncoding(File file) throws FileNotFoundException, IOException {
return guessFileEncoding(file, new nsDetector());
} /**
* <pre>
* 获取文件的编码
* @param file
* File对象实例
* @param languageHint
* 语言提示区域代码 @see #nsPSMDetector ,取值如下:
* 1 : Japanese
* 2 : Chinese
* 3 : Simplified Chinese
* 4 : Traditional Chinese
* 5 : Korean
* 6 : Dont know(default)
* </pre>
*
* @return 文件编码,eg:UTF-8,GBK,GB2312形式(不确定的时候,返回可能的字符编码序列);若无,则返回null
* @throws FileNotFoundException
* @throws IOException
*/
public String guessFileEncoding(File file, int languageHint) throws FileNotFoundException, IOException {
return guessFileEncoding(file, new nsDetector(languageHint));
} /**
* 获取文件的编码
*
* @param file
* @param det
* @return
* @throws FileNotFoundException
* @throws IOException
*/
private String guessFileEncoding(File file, nsDetector det) throws FileNotFoundException, IOException {
// Set an observer...
// The Notify() will be called when a matching charset is found.
det.Init(new nsICharsetDetectionObserver() {
public void Notify(String charset) {
encoding = charset;
found = true;
}
}); BufferedInputStream imp = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[1024];
int len;
boolean done = false;
boolean isAscii = false; while ((len = imp.read(buf, 0, buf.length)) != -1) {
// Check if the stream is only ascii.
isAscii = det.isAscii(buf, len);
if (isAscii) {
break;
}
// DoIt if non-ascii and not done yet.
done = det.DoIt(buf, len, false);
if (done) {
break;
}
}
imp.close();
det.DataEnd(); if (isAscii) {
encoding = "ASCII";
found = true;
} if (!found) {
String[] prob = det.getProbableCharsets();
//这里将可能的字符集组合起来返回
for (int i = 0; i < prob.length; i++) {
if (i == 0) {
encoding = prob[i];
} else {
encoding += "," + prob[i];
}
} if (prob.length > 0) {
// 在没有发现情况下,也可以只取第一个可能的编码,这里返回的是一个可能的序列
return encoding;
} else {
return null;
}
}
return encoding;
}
}
上面是判断文件编码的demo,本人测试了一下,得到的结果还是比较靠谱的~
上面提到的主页上还有一个HtmlCharsetDetector的demo,感兴趣的话可以去看一下。
java自动探测文件的字符编码的更多相关文章
- python学习笔记(2)--列表、元组、字符串、字典、集合、文件、字符编码
本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1.列表和元组的操作 列表是我们以后最长用的数据类型之一,通过列表可以最方便的对数据实现最方便的存储.修改等操作 定 ...
- Day2 - Python基础2 列表、字符串、字典、集合、文件、字符编码
本节内容 列表.元组操作 数字操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 ...
- python基础之 列表、元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码
本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 ...
- linux下改变文件的字符编码
首先确定文件的原始字符编码: $ file -bi test.txt 然后用 iconv 转换字符编码 $ iconv -f from-encoding -t to-encoding file > ...
- Gnu Linux下文件的字符编码及转换工具
/********************************************************************* * Author : Samson * Date ...
- eclipse设置新建jsp文件默认字符编码为utf-8
在使用Eclipse开发中,编码默认是ISO-8859-1,不支持中文.这样我们每次新建文件都要手动修改编码,非常麻烦.其实我们可以设置文件默认编码,今后再新建文件时就不用修改编码了. 1.打开Ecl ...
- fedora23深度配置gnome系统环境, 如设置ibus的面板字体大小 以及gedit 自动探测文件字符编码fileencodings
除了系统桌面gnome, 以及gnome应用程序自带的preferences, 还有很多设置, 没有在preferences, 而是被深度地隐藏在系统中, 这时, 需要安装 dconf-tools: ...
- python 读写文件和设置文件的字符编码
一. python打开文件代码如下: f = open("d:\test.txt", "w") 说明:第一个参数是文件名称,包括路径:第二个参数是打开的模式mo ...
- Java Web---登录验证和字符编码过滤器
什么是过滤器? 在Java Web中,过滤器即Filter.Servlet API中提供了一个Filter接口(javax.servlet.Filter).开发web应用时,假设编写的Java类实现了 ...
随机推荐
- 浏览器环境下JavaScript脚本加载与执行探析之动态脚本与Ajax脚本注入
在<浏览器环境下JavaScript脚本加载与执行探析之defer与async特性>中,我们研究了延迟脚本(defer)和异步脚本(async)的执行时机.浏览器支持情况.浏览器bug以及 ...
- C# Winform下一个热插拔的MIS/MRP/ERP框架(简介)
Programmer普弱哥们都喜欢玩自己的框架,我也不例外. 理想中,这个框架要易于理解.易于扩展.易于维护:最重要的,易于CODING. 系统是1主体框架+N模组的多个EXE/DLL组成的,在主体框 ...
- 拿 .properties 这种里面的数据 在不同的地方
1 在xml中 2 在.java中 @Value("#{configProperties['key']}") configProperties不是固定(根据配置时的id) ...
- Codeforces gym101612 L.Little Difference(枚举+二分)
传送:http://codeforces.com/gym/101612 题意:给定一个数n(<=1e18),将n分解为若干个数的成绩.要求这些数两两之间的差值不能大于1. 分析: 若n==2^k ...
- 在vue里面使用iVew框架
iVew框架的文档 https://www.iviewui.com/docs/guide/install 这里使用的是 npm 来安装,在项目下执行下面命令npm install iview -- ...
- maven封装jar包遇到的问题
使用eclipse编译后可以生成jar包,使用mvn clean package指令打包报错,错误如下:No compiler is provided in this environment. Per ...
- syslog之二:syslog协议及rsyslog服务全解析
目录: <syslog之一:Linux syslog日志系统详解> <syslog之二:syslog协议及rsyslog服务全解析> <syslog之三:建立Window ...
- vue使用代理实现开发阶段跨域
在config/index.js找到 proxyTable对象,添加键值对即可. "/api":{ target:"http://192.168.1.1", c ...
- 全网最详细的Windows系统里PLSQL Developer 64bit安装之后的一些配置(图文详解)
不多说,直接上干货! 注意的是: 本地若没有安装Oracle服务端,Oracle server服务端64位,是远程连接,因此本地配置PLSQL Developer64位. PLSQL Develope ...
- C#基础篇七类和静态成员
1.new关键字做的4个事情 1.1 开辟堆空间 a.开辟多大的空间呢? 当前类 所有的 成员变量类型所占空间的总和 + 类型指针(方法表的地址) b.开辟了空间干什么用呢? 存放 成员变量 1.2 ...