jChardet探测文件字符编码
2016-05-16 19:05
405 查看
package test; 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 火狐所用字符编码检测算法 public class FileCharsetDetector { private boolean found = false; private String encoding = null; public static void main(String[] argv) throws Exception { File file1 = new File("C:\\Users\\Administrator\\Desktop\\VideoViewDemo\\VideoViewDemo\\src\\org\\apache\\android\\media\\AudioPlayer.java"); 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; } }
参考文章:
http://www.cnblogs.com/yejg1212/p/3402322.html
相关文章推荐
- 命令行小技巧:读取文件的不同方式
- IE:临时文件保存法
- 文件的读出 编辑 管理
- 文件遍历排序函数
- 在线用表单建立文件夹
- VB获取文件大小的方法
- 文件、目录,文本文件等多种操作类
- 处理驱动器和文件夹
- 用vbs删除某些类型文件和磁盘空间报告的脚本
- Ruby实现批量对文件增加前缀代码分享
- C#获取文件夹及文件的大小与占用空间的方法
- delphi制作wav文件的方法
- PowerShell实现查询打开某个文件的默认应用程序
- bat 文件 学习使用指南
- IIS配置文件隐患
- Shell中删除某些文件外所有文件的3个方法
- winform实现拖动文件到窗体上的方法
- redis中修改配置文件中的端口号 密码方法
- 同步文件备份工具 Super Flexible File Synchronizer Pro v4
- C#获取文件创建时间的方法