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实现文件编码监测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: date js 半年_js Date 日
- 下一篇: C语言二维数组找出交集,【leetcod