首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java取得指定文件的编码类型

2012-10-28 
Java获得指定文件的编码类型import info.monitorenter.cpdetector.io.CodepageDetectorProxyimport info.

Java获得指定文件的编码类型

import info.monitorenter.cpdetector.io.CodepageDetectorProxy;import info.monitorenter.cpdetector.io.JChardetFacade;import java.io.File;  import java.nio.charset.Charset;  /** * 获得文件的编码类型 * 需要三个jar包:antlr.jar,chardet.jar,cpdetector_1.0.7.jar * */public class CharacterEnding {  public static void main(String[] args) {new CharacterEnding().getFileCharacterEnding("C://temps//zip压缩");  }/** * 获得文件的编码类型 * @param filePath * @return */public String getFileCharacterEnding(String filePath ) {  String fileCharacterEnding = "UTF-8";  CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();  detector.add(JChardetFacade.getInstance());  Charset charset = null;  File file = new File(filePath);  try {  charset = detector.detectCodepage(file.toURL());  } catch (Exception e) {  e.printStackTrace();  }  if (charset != null) {  fileCharacterEnding = charset.name();System.out.println(filePath+"的编码类型为:"+fileCharacterEnding);}  return fileCharacterEnding;  }  }  

热点排行