- 转载自:http://www.iteye.com/topic/540316
- 要用到三个包:chardet.jarantlr.jarcpdetector_1.0.7.jar
-
- importinfo.monitorenter.cpdetector.io.CodepageDetectorProxy;
- importinfo.monitorenter.cpdetector.io.JChardetFacade;
- importjava.io.File;
- importjava.nio.charset.Charset;
- publicclassCharacterEnding{
- publicstaticStringgetFileCharacterEnding(StringfilePath){
- Filefile=newFile(filePath);
- returngetFileCharacterEnding(file);
- }
- /**
- *Trytogetfilecharacterending.
- *</p>
- *<strong>Warning:</strong>usecpDetectortodetectfile'sencoding.
- *
- *@paramfile
- *@return
- */
- publicstaticStringgetFileCharacterEnding(Filefile){
- StringfileCharacterEnding="UTF-8";
- CodepageDetectorProxydetector=CodepageDetectorProxy.getInstance();
- detector.add(JChardetFacade.getInstance());
- Charsetcharset=null;
- //Filef=newFile(filePath);
- try{
- charset=detector.detectCodepage(file.toURL());
- }catch(Exceptione){
- e.printStackTrace();
- }
- if(charset!=null){
- fileCharacterEnding=charset.name();
- }
- returnfileCharacterEnding;
- }
- }
获取文件编码
最新推荐文章于 2022-08-06 20:48:08 发布