java自动探测文件的字符编码
来源:互联网 发布:betterzip 3.1.2 mac 编辑:程序博客网 时间:2024/05/29 15:08
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; }}
0 0
- java自动探测文件的字符编码
- Java自动探测文件的字符编码
- JAVA探测文件编码
- jChardet探测文件字符编码
- 关于文件的字符集编码探测--java
- 字符编码的概念及字符编码的探测
- 第六篇:JAVA探测文件编码
- 自动探测gbk编码文件并转为utf8
- xml如何探测字符编码
- icu 字符编码探测及字符编码转换实例
- JAVA字符的编码
- JAVA字符的编码
- Java的字符编码
- 探测文本文件编码的问题
- java文件编译出现“编码 GBK 的不可映射字符”
- JAVA字符的编码[转载]
- JAVA的默认字符编码
- java 字符编码的原理
- PyQt挖地雷游戏学习笔记(5)
- 网站的GOOGLE的PR=5了,挺惊讶的
- 《沉默的云》.读书笔记(一)
- QEventLoop 的使用两例
- 一切皆对象之两个方法概括js,无函数签名(无多态),原型,闭包,封装,引用类型,继承……
- java自动探测文件的字符编码
- 获取 Iphone 手机信息
- 坛肆谙侠思淄鞠事睦送赌杂粕vs
- 坛肆谙侠思淄鞠事睦送赌杂粕vs
- PHP文件的上传下载
- Codeforces Round #266 (Div. 2) A
- 【辛星理论体系】【第一】教育应该更加开放
- Codeforces Round #266 (Div. 2) B
- SQL语句中遇到的坑