JAVA探测文件编码

我们在做一些类似文件上传的功能时,用户上传的文件编码,我们是无法控制的,这是我们就需要对文件的编码进行探测。
示例代码如下:

import info.monitorenter.cpdetector.io.ASCIIDetector;
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;
import info.monitorenter.cpdetector.io.ParsingDetector;
import info.monitorenter.cpdetector.io.UnicodeDetector;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;

/**
 * 代码页探测器,需添加jar包:antlr-2.7.4.jar、chardet-1.0.jar、cpdetector_1.0.10.jar
 * 
 * @author jianggujin
 * 
 */
public class CodepageDetector
{
   /** 探测器代理 **/
   private CodepageDetectorProxy detector;

   /**
    * 构造方法, detector是探测器,它把探测任务交给具体的探测实现类的实例完成。
    * 
    * cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法
    * 
    * 加进来,如ParsingDetector、 JChardetFacade、ASCIIDetector、UnicodeDetector。
    * 
    * detector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的
    * 
    * 字符集编码。
    */
   public CodepageDetector()
   {
      detector = CodepageDetectorProxy.getInstance();
      // ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于
      // 指示是否显示探测过程的详细信息,为false不显示
      // 如果不希望判断xml的encoding,而是要判断该xml文件的编码,则可以注释掉
      detector.add(new ParsingDetector(false));
      // JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码测定。
      // 所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以再多加几个探测器,
      // 比如下面的ASCIIDetector、UnicodeDetector等。
      detector.add(JChardetFacade.getInstance());
      // ASCIIDetector用于ASCII编码测定
      detector.add(ASCIIDetector.getInstance());
      // UnicodeDetector用于Unicode家族编码的测定
      detector.add(UnicodeDetector.getInstance());
   }

   /**
    * 探测
    * 
    * @param file
    * @return charset
    * @throws MalformedURLException
    * @throws IOException
    */
   public Charset detector(File file) throws MalformedURLException, IOException
   {
      return detector.detectCodepage(file.toURI().toURL());
   }

   /**
    * 探测
    * 
    * @param url
    * @return charset
    * @throws IOException
    */
   public Charset detector(URL url) throws IOException
   {
      return detector.detectCodepage(url);
   }

   /**
    * 探测
    * 
    * @param inputStream
    * @param length
    *           长度应小于流的可用长度
    * @return charset
    * @throws IOException
    */
   public Charset detector(InputStream inputStream, int length)
         throws IOException
   {
      return detector.detectCodepage(inputStream, length);
   }

   public static void main(String[] args) throws IOException
   {
      File file = new File("test.txt");
      CodepageDetector detector = new CodepageDetector();
      System.out.println(detector.detector(file));
   }
}

我准备的文件test.txt文件的编码方式为:UTF-8
程序运行的结果如下:
UTF-8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值