麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

借助JCharDet獲取文件字符集

2019-11-14 22:18:59
字體:
來源:轉載
供稿:網友
借助JCharDet獲取文件字符集

前段時間,在學習lucene的時候,遇到了讀取txt文檔遇到編碼錯誤的問題。學了幾個解決方案,大部分是將文件轉十六進制(可以使用UE的Ctrl+H來查看),讀取開頭的四個標志位來判斷。可是總有些文本文件無法識別(我遇到的是部分使用UTF-8編碼的文件),后來發現了JCharDet。JCharDet是mozilla(就是Firefox那家)的編碼識別算法的java實現,算了,這里是官網,自己看吧。

上代碼:

package com.zhyea.util;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獲取文件字符集 *  * @author robin *  */public class FileCharsetDetector {    /**     * 字符集名稱     */    PRivate static String encoding;        /**     * 字符集是否已檢測到     */    private static boolean found;        private static nsDetector detector;        private static nsICharsetDetectionObserver observer;    /**     * 適應語言枚舉     * @author robin     *     */    enum Language{        Japanese(1),        Chinese(2),        SimplifiedChinese(3),        TraditionalChinese(4),         Korean(5),         DontKnow(6);                private int hint;                Language(int hint){            this.hint = hint;        }                public int getHint(){            return this.hint;        }    }        /**     * 傳入一個文件(File)對象,檢查文件編碼     *      * @param file     *            File對象實例     * @return 文件編碼,若無,則返回null     * @throws FileNotFoundException     * @throws IOException     */    public static String checkEncoding(File file) throws FileNotFoundException,            IOException {        return checkEncoding(file, getNsdetector());    }    /**     * 獲取文件的編碼     *      * @param file     *            File對象實例     * @param language     *            語言     * @return 文件編碼     * @throws FileNotFoundException     * @throws IOException     */    public static String checkEncoding(File file, Language lang)            throws FileNotFoundException, IOException {        return checkEncoding(file, new nsDetector(lang.getHint()));    }    /**     * 獲取文件的編碼     *      * @param path     *            文件路徑     * @return 文件編碼,eg:UTF-8,GBK,GB2312形式,若無,則返回null     * @throws FileNotFoundException     * @throws IOException     */    public static String checkEncoding(String path) throws FileNotFoundException,            IOException {        return checkEncoding(new File(path));    }    /**     * 獲取文件的編碼     *      * @param path     *            文件路徑     * @param language     *                 語言     * @return     * @throws FileNotFoundException     * @throws IOException     */    public static String checkEncoding(String path, Language lang)            throws FileNotFoundException, IOException {        return checkEncoding(new File(path), lang);    }    /**     * 獲取文件的編碼     *      * @param file     * @param det     * @return     * @throws FileNotFoundException     * @throws IOException     */    private static String checkEncoding(File file, nsDetector detector)            throws FileNotFoundException, IOException {                detector.Init(getCharsetDetectionObserver());                if (isAscii(file, detector)) {            encoding = "ASCII";            found = true;        }        if (!found) {            String prob[] = detector.getProbableCharsets();            if (prob.length > 0) {                encoding = prob[0];            } else {                return null;            }        }                return encoding;    }        /**     * 檢查文件編碼類型是否是ASCII型     * @param file     *             要檢查編碼的文件     * @param detector     * @return     * @throws IOException     */    private static boolean isAscii(File file, nsDetector detector) throws IOException{        BufferedInputStream input = null;        try{            input = new BufferedInputStream(new FileInputStream(file));                        byte[] buffer = new byte[1024];            int hasRead;            boolean done = false;            boolean isAscii = true;            while ((hasRead=input.read(buffer)) != -1) {                if (isAscii)                    isAscii = detector.isAscii(buffer, hasRead);                if (!isAscii && !done)                    done = detector.DoIt(buffer, hasRead, false);            }                        return isAscii;        }finally{            detector.DataEnd();            if(null!=input)input.close();        }    }        /**     * nsDetector單例創建     * @return     */    private static nsDetector getNsdetector(){        if(null == detector){            detector = new nsDetector();        }        return detector;    }        /**     * nsICharsetDetectionObserver 單例創建     * @return     */    private static nsICharsetDetectionObserver getCharsetDetectionObserver(){        if(null==observer){            observer = new nsICharsetDetectionObserver() {                public void Notify(String charset) {                    found = true;                    encoding = charset;                }            };        }        return observer;    }}

這個還存一個問題,就是識別Unicode編碼的文件,會返回windows-1252。我使用windows-1252作為編碼的時候會報錯。

對了,再提供一個這個jar包下載的地址,官網有時會抽風,不能訪問。

下載地址:http://download.csdn.net/detail/tianxiexingyun/8286849

就這樣。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久精品亚洲一区二区三区观看模式 | 毛片一级视频 | 福利在线小视频 | 国产瑟瑟视频 | 成人在线观看一区二区三区 | 成人免费视频视频在线观看 免费 | 怦然心动50免费完整版 | 一区国产在线 | 成人青青草| 成年免费大片黄在线观看岛国 | 欧美日本亚洲视频 | 美女羞羞视频网站 | 国产精品久久久久影院老司 | 欧美精品一区二区三区在线 | 欧美成人久久 | 欧美精品v国产精品v日韩精品 | 免费a级网站 | 精品亚洲一 | 香蕉国产精品 | 日韩在线欧美在线 | 国产精品99精品 | 欧洲精品视频在线观看 | 成人免费毛片片v | 激情毛片| 欧美成人精品一区二区 | 国产激情视频在线 | 一夜新娘第三季免费观看 | 精品国产乱码久久久久久丨区2区 | 欧美精品成人一区二区在线观看 | 久久久久久久国产视频 | 嗯~啊~弄嗯~啊h高潮视频 | 懂色av懂色aⅴ精彩av | 中文字幕精品一区久久久久 | 日韩欧美高清一区 | 精品一二三区视频 | 久久久久久麻豆 | 国产99久久精品一区二区300 | 91九色免费视频 | 欧美日韩国产成人在线观看 | 午夜噜噜噜 | 色玖玖综合 |