zoukankan      html  css  js  c++  java
  • java自动探测文件的字符编码

    Mozilla有一个C++版的自动字符集探测算法代码,然后sourceforge上有人将其改成java版的~~

    主页:http://jchardet.sourceforge.net/

    jchardet is a java port of the source from mozilla's automatic charset detection algorithm.
    The original author is Frank Tang. What is available here is the java port of that code.
    The original source in C++ can be found from http://lxr.mozilla.org/mozilla/source/intl/chardet/
    More information can be found at http://www.mozilla.org/projects/intl/chardet.html

    下面是见证奇迹的时刻:

    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;
        }
    }

    上面是判断文件编码的demo,本人测试了一下,得到的结果还是比较靠谱的~

    上面提到的主页上还有一个HtmlCharsetDetector的demo,感兴趣的话可以去看一下。

  • 相关阅读:
    前端 HTML
    python3内置函数
    内置函数的随机验证码
    线程、进程以及协程,上下文管理器
    线程池的定义方法
    python_控制台输出带颜色的文字方法
    while 循环 continue break 用法例子
    JVM 基础知识
    ios 设置状态栏文本颜色为白色
    ios 常用第三方库要加的framework,ARC的设置
  • 原文地址:https://www.cnblogs.com/yejg1212/p/3402322.html
Copyright © 2011-2022 走看看