日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

chardet java_java实现文件编码监测

發布時間:2024/7/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 chardet java_java实现文件编码监测 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java實現文件編碼監測

最近在做一個文檔的翻譯項目,可文檔的編碼不知道,聽頭疼的。嘗試了很多方法最后發現JCharDet這個工具可以輕松解決這個問題。于是作此筆記希望日后提醒自己以及幫助又需要的人。

package com.uujava.mbfy.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;

/**********************************************

* Maven

*

*

*net.sourceforge.jchardet

*jchardet

*1.0

*

* *********************************************/

/**

* 借助JCharDet獲取文件字符集 JCharDet

* 是mozilla自動字符集探測算法代碼的java移植,其官方主頁為:

* http://jchardet.sourceforge.net/

*/

public class FileCharsetDetector {

private boolean found = false;

/**

* 如果完全匹配某個字符集檢測算法, 則該屬性保存該字符集的名稱.

* 否則(如二進制文件)其值就為默認值 null, 這時應當查詢屬性

*/

private String encoding = null;

public static void main(String[] argv) throws Exception {

System.out

.println("文件編碼:"

+ new FileCharsetDetector()

.guestFileEncoding("/home/k/Documents/test/azmind_7_xh/azmind_7_xh/路由管理.txt"));

}

/**

* 傳入一個文件(File)對象,檢查文件編碼

*

* @param file

* File對象實例

* @return 文件編碼,若無,則返回null

* @throws FileNotFoundException

* @throws IOException

*/

public String guestFileEncoding(File file) throws FileNotFoundException,

IOException {

return geestFileEncoding(file, new nsDetector());

}

/**

* 獲取文件的編碼

*

* @param file

* File對象實例

* @param languageHint

* 語言提示區域代碼 eg:1 : Japanese; 2 : Chinese; 3 : Simplified Chinese;

* 4 : Traditional Chinese; 5 : Korean; 6 : Dont know (default)

* @return 文件編碼,eg:UTF-8,GBK,GB2312形式,若無,則返回null

* @throws FileNotFoundException

* @throws IOException

*/

public String guestFileEncoding(File file, int languageHint)

throws FileNotFoundException, IOException {

return geestFileEncoding(file, new nsDetector(languageHint));

}

/**

* 獲取文件的編碼

*

* @param path

* 文件路徑

* @return 文件編碼,eg:UTF-8,GBK,GB2312形式,若無,則返回null

* @throws FileNotFoundException

* @throws IOException

*/

public String guestFileEncoding(String path) throws FileNotFoundException,

IOException {

return guestFileEncoding(new File(path));

}

/**

* 獲取文件的編碼

*

* @param path

* 文件路徑

* @param languageHint

* 語言提示區域代碼 eg:1 : Japanese; 2 : Chinese; 3 : Simplified Chinese;

* 4 : Traditional Chinese; 5 : Korean; 6 : Dont know (default)

* @return

* @throws FileNotFoundException

* @throws IOException

*/

public String guestFileEncoding(String path, int languageHint)

throws FileNotFoundException, IOException {

return guestFileEncoding(new File(path), languageHint);

}

/**

* 獲取文件的編碼

*

* @param file

* @param det

* @return

* @throws FileNotFoundException

* @throws IOException

*/

private String geestFileEncoding(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) {

found = true;

encoding = charset;

}

});

BufferedInputStream imp = new BufferedInputStream(new FileInputStream(file));

byte[] buf = new byte[1024];

int len;

boolean done = false;

boolean isAscii = true;

while ((len = imp.read(buf, 0, buf.length)) != -1) {

// Check if the stream is only ascii.

if (isAscii)

isAscii = det.isAscii(buf, len);

// DoIt if non-ascii and not done yet.

if (!isAscii && !done)

done = det.DoIt(buf, len, false);

}

det.DataEnd();

if (isAscii) {

encoding = "ASCII";

found = true;

}

if (!found) {

String prob[] = det.getProbableCharsets();

if (prob.length > 0) {

// 在沒有發現情況下,則取第一個可能的編碼

encoding = prob[0];

} else {

return null;

}

}

return encoding;

}

}

總結

以上是生活随笔為你收集整理的chardet java_java实现文件编码监测的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。