六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 83|回复: 0

判定文件编码或文本流编码的方法

[复制链接]

升级  20%

20

主题

20

主题

20

主题

秀才

Rank: 2

积分
80
 楼主| 发表于 2013-2-7 15:28:55 | 显示全部楼层 |阅读模式
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.ByteArrayInputStream;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.nio.charset.Charset;/** * <p> * 本类用来探测字符的编码集,关返回其名称 * </p> *  * @ * @vision 1.0 */public class Detector {/*------------------------------------------------------------------------   detectorProxy是探测器,它把探测任务交给具体的探测实现类的实例完成。   cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法   加进来,如ParsingDetector、 JChardetFacade、ASCIIDetector、UnicodeDetector。     detector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的   字符集编码。 --------------------------------------------------------------------------*/private static CodepageDetectorProxy detectorProxy;static {detectorProxy = CodepageDetectorProxy.getInstance();/*-------------------------------------------------------------------------   ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于   指示是否显示探测过程的详细信息,为false不显示。 ---------------------------------------------------------------------------*/detectorProxy.add(new ParsingDetector(false));/*--------------------------------------------------------------------------   JChardetFacade封装了由mozilla1组织提供的JChardet,它可以完成大多数文件的编码   测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以   再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。  ---------------------------------------------------------------------------*/detectorProxy.add(JChardetFacade.getInstance());// ASCIIDetector用于ASCII编码测定detectorProxy.add(ASCIIDetector.getInstance());// UnicodeDetector用于unicode1家族编码的测定detectorProxy.add(UnicodeDetector.getInstance());}public static synchronized String getEncodingType(String content)throws IllegalArgumentException, IOException {ByteArrayInputStream stream = new ByteArrayInputStream(content.getBytes());return Detector.getEncodingType(stream, content.length());}public static synchronized String getEncodingType(File file)throws MalformedURLException, IOException {Charset charset = detectorProxy.detectCodepage(file.toURL());if (charset != null) {return charset.name();} elsereturn "未知";}public static synchronized String getEncodingType(InputStream inputStream,int length) throws IllegalArgumentException, IOException {Charset charset = detectorProxy.detectCodepage(inputStream, length);if (charset != null) {return charset.name();} elsereturn "未知";}} 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表