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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

java文件file字符集_获取文件字符集(或文件编码) 的工具类

發(fā)布時間:2025/3/20 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java文件file字符集_获取文件字符集(或文件编码) 的工具类 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

packageorg.mozilla.intl.chardet;

importjava.io.BufferedInputStream;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileNotFoundException;

importjava.io.IOException;

/**

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

*?@author?icer

*?PS:

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

*??????http://jchardet.sourceforge.net/

*?@date????2008/11/13

*/

publicclassFileCharsetDetector?{

privatebooleanfound?=false;

/**

*?如果完全匹配某個字符集檢測算法,?則該屬性保存該字符集的名稱.?否則(如二進制文件)其值就為默認值?null,?這時應當查詢屬性

*/

privateString?encoding?=null;

publicstaticvoidmain(String[]?argv)throwsException?{

if(argv.length?!=1&&?argv.length?!=2)?{

System.out

.println("Usage:?FileCharsetDetector??[]");

System.out.println("");

System.out.println("Where??is?d:/demo.txt");

System.out.println("For?optional?.?Use?following...");

System.out.println("????????1?=>?Japanese");

System.out.println("????????2?=>?Chinese");

System.out.println("????????3?=>?Simplified?Chinese");

System.out.println("????????4?=>?Traditional?Chinese");

System.out.println("????????5?=>?Korean");

System.out.println("????????6?=>?Dont?know?(default)");

return;

}?else{

String?encoding?=?null;

if(argv.length?==2)?{

encoding?=?newFileCharsetDetector().guestFileEncoding(argv[0],

Integer.valueOf(argv[1]));

}?else{

encoding?=?newFileCharsetDetector().guestFileEncoding(argv[0]);

}

System.out.println("文件編碼:"+?encoding);

}

}

/**

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

*

*?@param?file

*????????????File對象實例

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

*?@throws?FileNotFoundException

*?@throws?IOException

*/

publicString?guestFileEncoding(File?file)throwsFileNotFoundException,

IOException?{

returngeestFileEncoding(file,newnsDetector());

}

/**

*?獲取文件的編碼

*

*?@param?file

*????????????File對象實例

*?@param?languageHint

*????????????語言提示區(qū)域代碼?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

*/

publicString?guestFileEncoding(File?file,intlanguageHint)

throwsFileNotFoundException,?IOException?{

returngeestFileEncoding(file,newnsDetector(languageHint));

}

/**

*?獲取文件的編碼

*

*?@param?path

*????????????文件路徑

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

*?@throws?FileNotFoundException

*?@throws?IOException

*/

publicString?guestFileEncoding(String?path)throwsFileNotFoundException,

IOException?{

returnguestFileEncoding(newFile(path));

}

/**

*?獲取文件的編碼

*

*?@param?path

*????????????文件路徑

*?@param?languageHint

*????????????語言提示區(qū)域代碼?eg:1?:?Japanese;?2?:?Chinese;?3?:?Simplified?Chinese;

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

*?@return

*?@throws?FileNotFoundException

*?@throws?IOException

*/

publicString?guestFileEncoding(String?path,intlanguageHint)

throwsFileNotFoundException,?IOException?{

returnguestFileEncoding(newFile(path),?languageHint);

}

/**

*?獲取文件的編碼

*

*?@param?file

*?@param?det

*?@return

*?@throws?FileNotFoundException

*?@throws?IOException

*/

privateString?geestFileEncoding(File?file,?nsDetector?det)

throwsFileNotFoundException,?IOException?{

//?Set?an?observer...

//?The?Notify()?will?be?called?when?a?matching?charset?is?found.

det.Init(newnsICharsetDetectionObserver()?{

publicvoidNotify(String?charset)?{

found?=?true;

encoding?=?charset;

}

});

BufferedInputStream?imp?=?newBufferedInputStream(newFileInputStream(

file));

byte[]?buf?=newbyte[1024];

intlen;

booleandone?=false;

booleanisAscii?=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)?{

//?在沒有發(fā)現(xiàn)情況下,則取第一個可能的編碼

encoding?=?prob[0];

}?else{

returnnull;

}

}

returnencoding;

}

}

網上有一些使用文章,但是使用起來,發(fā)現(xiàn)還是有一些問題的,看著官方實例,就自己寫了一個。使用起來,感覺不錯。

附件為jchardet包。

總結

以上是生活随笔為你收集整理的java文件file字符集_获取文件字符集(或文件编码) 的工具类的全部內容,希望文章能夠幫你解決所遇到的問題。

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