jChardet探测文件字符编码

简介: 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.char
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


本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1774032

目录
相关文章
|
3月前
|
JSON JavaScript Linux
有关于【该文件的字符编码需要在传输协议层声明,或者在文件中加入一个 BOM(字节顺序标记)】的另一种解决办法
有关于【该文件的字符编码需要在传输协议层声明,或者在文件中加入一个 BOM(字节顺序标记)】的另一种解决办法
|
Oracle 关系型数据库 Java
解决读取Oracle数据库US7ASCII编码乱码问题
今天和第三方对接数据时,对方提供了一个视图US7ASCII编码,给代码调试带来了很大的不便。程序输出的mybatis获取的对象及new String(s.getBytes("ISO8859-1"), "GB2312")加解密后都是乱码。
1445 1
2 字节的 UTF-8 序列的字节 2 无效 解决方法
2 字节的 UTF-8 序列的字节 2 无效 解决方法: 用记事本打开xml文件,另存为 编码 选择 UTF-8,保存替换掉之前的文件,解决问题博客内容仅代表个人观点,如发现阐述有误,麻烦指正,谢谢!
4058 0
AVI格式视频文件编码格式缺少编码解释器且该项目的编码格式不受支持(0xc00d5212错误)
AVI格式视频文件编码格式缺少编码解释器且该项目的编码格式不受支持(0xc00d5212错误)
1841 0
AVI格式视频文件编码格式缺少编码解释器且该项目的编码格式不受支持(0xc00d5212错误)
类似%-30的字串解码办法
类似%-30的字串解码办法
47 0
|
应用服务中间件
设置请求编码
设置编码,改成我们习惯的中文输出
错误: 编码GBK的不可映射字符
错误: 编码GBK的不可映射字符
通过二进制头识别文件类型
通过二进制头识别文件类型,可以使用UE或者WinHex软件打开 1.JPEG/JPG - 文件头标识 (2 bytes): $ff, $d8 (SOI) (JPEG文件标识)  - 文件结束标识 (2 bytes): $ff, $d9 (EOI)  2.
1799 0
|
存储
编码问题(总结网络的)
什么是编码? 计算机中存储的都是二进制,但是要显示的时候,就是我们看到的却可以有中国 ,a 1 等字符 计算机中是没有存储字符的,但是我们却看到了。
1178 0