使用JCharDet,获得文件的字符集编码!
来源:互联网 发布:java ssm框架项目实例 编辑:程序博客网 时间:2024/05/29 02:38
需要jchardet-1.0.jar包。
package p;
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;
/**
* 借助JCharDet获取文件字符集 JCharDet 是mozilla自动字符集探测算法代码的java移植,其官方主页为:
* http://jchardet.sourceforge.net/
*/
public class FileCharsetDetector {
private boolean found = false;
/**
* 如果完全匹配某个字符集检测算法, 则该属性保存该字符集的名称. 否则(如二进制文件)其值就为默认值 null, 这时应当查询属性
*/
private String encoding = null;
public static void main(String[] argv) throws Exception {
System.out.println("文件编码:"
+ new FileCharsetDetector().guestFileEncoding("C:\\测试1.txt"));
}
/**
* 传入一个文件(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;
}
}
- 使用JCharDet,获得文件的字符集编码!
- 使用 jchardet 获得文件编码 -javacode
- 字符集编码的自动识别jchardet
- 字符集编码的自动识别jchardet
- Java字符集编码的自动识别 jchardet
- jChardet探测文件字符编码
- java使用jchardet检测文本文件(字节流)的编码方式
- java字符集探测器jchardet
- 基于JChardet自动检测文件编码,批量转换文件编码,解决乱码问题
- 基于JChardet自动检测文件编码,批量转换文件编码,解决乱码问题
- Java 判断文件的字符集编码
- 关于文件的字符集编码探测--java
- 浅谈java使用指定字符集编码,以及常见的字符集
- jchardet
- 编码字符集与字符集编码的区别
- 编码字符集与字符集编码的区别
- 编码字符集与字符集编码的区别
- 编码字符集与字符集编码的区别
- Java Graphics_12
- CRT detected that the application wrote to memory after end of heap buffer
- jquery的一些基本的用法
- applicationWillResignActive
- python gearman的应用
- 使用JCharDet,获得文件的字符集编码!
- js中cookie的添加,删除,查询
- 有关Mat的函数
- 软件测试意识之主动思考
- BlackBerry 企业成功案例
- Android 通用获取Ip的方法(判断手机是否联网的方法)!!!
- 双向一对多
- Movable Type简介
- asp.net url编码问题处理