zoukankan      html  css  js  c++  java
  • cpdetector获取文件编码

     1 import info.monitorenter.cpdetector.io.ASCIIDetector;
     2 import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
     3 import info.monitorenter.cpdetector.io.JChardetFacade;
     4 import info.monitorenter.cpdetector.io.ParsingDetector;
     5 import info.monitorenter.cpdetector.io.UnicodeDetector;
     6 
     7 import java.io.File;
     8 
     9 /**
    10  * @author zhanghf E-mail:zhanghf@yintong.com.cn
    11  * @version 创建时间:2014-3-26 上午11:59:36 
    12  * 获取文件编码类
    13  */
    14 public class EncodeUtil {
    15 
    16     /**
    17      * 利用第三方开源包cpdetector获取文件编码格式
    18      * 
    19      * @param path
    20      *            要判断文件编码格式的源文件的路径
    21      * @author huanglei
    22      * @version 2012-7-12 14:05
    23      */
    24     public static String getFileEncode(String path) {
    25         /*
    26          * detector是探测器,它把探测任务交给具体的探测实现类的实例完成。
    27          * cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法 加进来,如ParsingDetector、
    28          * JChardetFacade、ASCIIDetector、UnicodeDetector。
    29          * detector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的
    30          * 字符集编码。使用需要用到三个第三方JAR包:antlr.jar、chardet.jar和cpdetector.jar
    31          * cpDetector是基于统计学原理的,不保证完全正确。
    32          */
    33         CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
    34         /*
    35          * ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于
    36          * 指示是否显示探测过程的详细信息,为false不显示。
    37          */
    38         detector.add(new ParsingDetector(false));
    39         /*
    40          * JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码
    41          * 测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以
    42          * 再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。
    43          */
    44         detector.add(JChardetFacade.getInstance());// 用到antlr.jar、chardet.jar
    45         // ASCIIDetector用于ASCII编码测定
    46         detector.add(ASCIIDetector.getInstance());
    47         // UnicodeDetector用于Unicode家族编码的测定
    48         detector.add(UnicodeDetector.getInstance());
    49         java.nio.charset.Charset charset = null;
    50         File f = new File(path);
    51         try {
    52             charset = detector.detectCodepage(f.toURI().toURL());
    53         } catch (Exception ex) {
    54             ex.printStackTrace();
    55         }
    56         if (charset != null)
    57             return charset.name();
    58         else
    59             return null;
    60     }
    61 
    62 }
  • 相关阅读:
    es字符串字段类型的私有属性 建立索引的分析器 用于查询的分析器 短语查询优化
    限制索引、字段的自动创建 无模式与自定义 索引结构映射 切面搜索
    Lucene索引数计算
    API网关性能比较:NGINX vs. ZUUL vs. Spring Cloud Gateway vs. Linkerd API 网关出现的原因
    git 中 A C D M R T U X 分别解释
    通过 ulimit 改善系统性能
    机器重启 查看crontab执行历史记录crontab没有执行
    烂代码 git blame
    es 中 for in for of
    发散阶段 收集阶段 标识符 得分 副本角色
  • 原文地址:https://www.cnblogs.com/cxyzl/p/3627965.html
Copyright © 2011-2022 走看看