java 讀取不同編碼的txt檔案 中文亂碼二

來源:互聯網
上載者:User

標籤:guess   使用   param   java   情況   ini   private   data   mat   

之前的文章中判斷txt的編碼,發現utf-8無BOM編碼格式無法檢測出來。

當無法檢測時(返回的code為空白時),再使用一下方法則可以了。

 /**     * 傳入一個檔案(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;    }

  

java 讀取不同編碼的txt檔案 中文亂碼二

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.