`
tansitongba
  • 浏览: 485324 次
文章分类
社区版块
存档分类
最新评论

获取文件编码

 
阅读更多
  1. 转载自:http://www.iteye.com/topic/540316
  2. 要用到三个包:chardet.jarantlr.jarcpdetector_1.0.7.jar

  3. importinfo.monitorenter.cpdetector.io.CodepageDetectorProxy;
  4. importinfo.monitorenter.cpdetector.io.JChardetFacade;
  5. importjava.io.File;
  6. importjava.nio.charset.Charset;
  7. publicclassCharacterEnding{
  8. publicstaticStringgetFileCharacterEnding(StringfilePath){
  9. Filefile=newFile(filePath);
  10. returngetFileCharacterEnding(file);
  11. }
  12. /**
  13. *Trytogetfilecharacterending.
  14. *</p>
  15. *<strong>Warning:</strong>usecpDetectortodetectfile'sencoding.
  16. *
  17. *@paramfile
  18. *@return
  19. */
  20. publicstaticStringgetFileCharacterEnding(Filefile){
  21. StringfileCharacterEnding="UTF-8";
  22. CodepageDetectorProxydetector=CodepageDetectorProxy.getInstance();
  23. detector.add(JChardetFacade.getInstance());
  24. Charsetcharset=null;
  25. //Filef=newFile(filePath);
  26. try{
  27. charset=detector.detectCodepage(file.toURL());
  28. }catch(Exceptione){
  29. e.printStackTrace();
  30. }
  31. if(charset!=null){
  32. fileCharacterEnding=charset.name();
  33. }
  34. returnfileCharacterEnding;
  35. }
  36. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics