//使用 jchardet 获得文件编码 -javacode
//当含中文的文件用ANSI编码保存时,检测还是出错。
package org.mozilla.intl.chardet;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* 借助JCharDet获取文件字符集
* @author icer
* PS:
* JCharDet 是mozilla自动字符集探测算法代码的java移植,其官方主页为:
* http://jchardet.sourceforge.net/
* @date 2008/11/13
*/
public class FileCharsetDetector {
private boolean found = false;
/**
* 如果完全匹配某个字符集检测算法, 则该属性保存该字符集的名称. 否则(如二进制文件)其值就为默认值 null, 这时应当查询属性
*/
private String encoding = null;
public static void main(String[] argv) throws Exception {
if (argv.length != 1 && argv.length != 2) {
System.out
.println("Usage: FileCharsetDetector <path> [<languageHint>]");
System.out.println("");
System.out.println("Where <path> is d:/demo.txt");
System.out.println("For optional <languageHint>. Use following...");
System.out.println(" 1 => Japanese");
System.out.println(" 2 => Chinese");
System.out.println(" 3 => Simplified Chinese");
System.out.println(" 4 => Traditional Chinese");
System.out.println(" 5 => Korean");
System.out.println(" 6 => Dont know (default)");
return;
} else {
String encoding = null;
if (argv.length == 2) {
encoding = new FileCharsetDetector().guestFileEncoding(argv[0],
Integer.valueOf(argv[1]));
} else {
encoding = new FileCharsetDetector().guestFileEncoding(argv[0]);
}
System.out.println("文件编码:" + encoding);
}
}
/**
* 传入一个文件(File)对象,检查文件编码
*
* @param file
* File对象实例
* @return 文件编码,若无,则返回null
* @throws FileNotFoundException
* @throws IOException
*/
public String guestFileEncoding(File file) throws FileNotFoundException,
IOException {
return geestFileEncoding(file, new nsDetector());
}
/**
* 获取文件的编码
*
* @param file
* File对象实例
* @param languageHint
* 语言提示区域代码 eg:1 : Japanese; 2 : Chinese; 3 : Simplified Chinese;
* 4 : Traditional Chinese; 5 : Korean; 6 : Dont know (default)
* @return 文件编码,eg:UTF-8,GBK,GB2312形式,若无,则返回null
* @throws FileNotFoundException
* @throws IOException
*/
public String guestFileEncoding(File file, int languageHint)
throws FileNotFoundException, IOException {
return geestFileEncoding(file, new nsDetector(languageHint));
}
/**
* 获取文件的编码
*
* @param path
* 文件路径
* @return 文件编码,eg:UTF-8,GBK,GB2312形式,若无,则返回null
* @throws FileNotFoundException
* @throws IOException
*/
public String guestFileEncoding(String path) throws FileNotFoundException,
IOException {
return guestFileEncoding(new File(path));
}
/**
* 获取文件的编码
*
* @param path
* 文件路径
* @param languageHint
* 语言提示区域代码 eg:1 : Japanese; 2 : Chinese; 3 : Simplified Chinese;
* 4 : Traditional Chinese; 5 : Korean; 6 : Dont know (default)
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public String guestFileEncoding(String path, int languageHint)
throws FileNotFoundException, IOException {
return guestFileEncoding(new File(path), languageHint);
}
/**
* 获取文件的编码
*
* @param file
* @param det
* @return
* @throws FileNotFoundException
* @throws IOException
*/
private String geestFileEncoding(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) {
found = true;
encoding = charset;
}
});
BufferedInputStream imp = new BufferedInputStream(new FileInputStream(
file));
byte[] buf = new byte[1024];
int len;
boolean done = false;
boolean isAscii = true;
while ((len = imp.read(buf, 0, buf.length)) != -1) {
// Check if the stream is only ascii.
if (isAscii)
isAscii = det.isAscii(buf, len);
// DoIt if non-ascii and not done yet.
if (!isAscii && !done)
done = det.DoIt(buf, len, false);
}
det.DataEnd();
if (isAscii) {
encoding = "ASCII";
found = true;
}
if (!found) {
String prob[] = det.getProbableCharsets();
if (prob.length > 0) {
// 在没有发现情况下,则取第一个可能的编码
encoding = prob[0];
} else {
return null;
}
}
return encoding;
}
}
分享到:
相关推荐
NULL 博文链接:https://zhifeiji512.iteye.com/blog/1221068
jchardet-1.1 字符编码识别jchardet-1.1 字符编码识jchardet是mozilla自动字Java字符串(及字符)类以Unicode编码保存数据。当处理来自外部的国际性文本时,我们需要提供关于这些文本的编码,以便准确地将它们转换为...
jchardet-1.1 字符编码识别jchardet-1.1 字符编码识jchardet是mozilla自动字Java字符串(及字符)类以Unicode编码保存数据。当处理来自外部的国际性文本时,我们需要提供关于这些文本的编码,以便准确地将它们转换为...
NULL 博文链接:https://nopainnogain.iteye.com/blog/771041
一个用来检查文件编码字符集的工具,使用jchardet作为编码检测的组件!
jchardet-1.0.jar java判断文件字符编码的类库!
jchardet-1.1.zip jchardet-1.1.zip 包含源码及jar。 jchardet-1.1.zip
鱼泽弋海 封装的Jchardet类以及chardet.jar,感觉是网上找到的例子里写的很好的。 Jchardet,会返回一个最有可能的charset,和一组其他可能,正确率还行,但在数据很少的情况下,出错率会变高,可以用RadioGroup...
一个 mozilla 的 编码检测 java 包.
使用cpdetector 三方库读取文件编码。 jar包和事例代码都有。
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
作用:检测文本文件的编码方式,例如GB2312、UTF-8、Shift-jis、ISO-8859-1等等。相关讨论可以参考http://blog.csdn.net/wingtrace/archive/2007/03/17/1532262.aspx 。 用法:请参考GenericCharsetDetector类的几...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码 测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以 再多加几个探测器,比如下面的ASCIIDetector...
爬虫中经常遇到采集网页乱码问题,此时程序根据meta、header无法识别情况下,下载需要的包 antlr.jar、cpdetector-1.0.10.jar、jchardet-1.0.jar