Exemplo n.º 1
0
 /**
  * 使用Cpdetector检测文件编码
  *
  * @param file
  * @return
  */
 public static Charset getFileEncode(File file) {
   try {
     CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
     detector.add(new ParsingDetector(false));
     detector.add(JChardetFacade.getInstance());
     detector.add(ASCIIDetector.getInstance());
     detector.add(UnicodeDetector.getInstance());
     java.nio.charset.Charset charset = null;
     charset = detector.detectCodepage(file.toURI().toURL());
     return charset;
   } catch (Exception ex) {
     ex.printStackTrace();
     return null;
   }
 }
Exemplo n.º 2
0
/**
 * Project: com.zhuanleme.algorithmImpl
 *
 * <p>Title: CpDetector.java
 *
 * <p>
 *
 * <p>Description: CpDetector
 *
 * <p>
 *
 * <p>Copyright: Copyright (c) 2015
 *
 * <p>
 *
 * @author zhangdihong
 * @version 1.0
 * @date 2015/10/28
 */
public class CpDetector {

  public static CodepageDetectorProxy codepageDetectorProxy = CodepageDetectorProxy.getInstance();

  static {
    codepageDetectorProxy.add(
        new ParsingDetector(
            false)); // ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于指示是否显示探测过程的详细信息,为false不显示。
    codepageDetectorProxy.add(
        JChardetFacade
            .getInstance()); // JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码
                             // 测定.比如下面的ASCIIDetector、UnicodeDetector等
    codepageDetectorProxy.add(ASCIIDetector.getInstance()); // ASCIIDetector用于ASCII编码测定
    codepageDetectorProxy.add(UnicodeDetector.getInstance()); // UnicodeDetector用于Unicode家族编码的测定
  }
}