`
hz_chenwenbiao
  • 浏览: 994578 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

java使用jchardet检测文本文件(字节流)的编码方式(转)

阅读更多

有时需要InputStreamReader(InputStream in, Charset cs)这个构造来处理字符流。然而Charset不一定知道。这个时候就需要检测编码方式了。jchardet是firefox使用的字节流编码检测算法的java开源实现,协议为MPL(Mozilla Public License),对商业友好。下载源代码后发现示例并不怎么好使用,于是封装了一下。下面就封装类和使用Demo。

 

CharsetDetector 这个封装了内部实现,用户直接new这个类就可以检测字节流编码

 

package cn.xddai.chardet;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.mozilla.intl.chardet.nsDetector;
import org.mozilla.intl.chardet.nsICharsetDetectionObserver;
import org.mozilla.intl.chardet.nsPSMDetector;

/**
 *
 * @author xddai
 */
public class CharsetDetector
{

    private boolean found = false;
    private String result;
    private int lang;

    public String[] detectChineseCharset(InputStream in) throws IOException
    {
        lang = nsPSMDetector.CHINESE;
        String[] prob;
        // Initalize the nsDetector() ;
        nsDetector det = new nsDetector(lang);
        // Set an observer...
        // The Notify() will be called when a matching charset is found.

        det.Init(new nsICharsetDetectionObserver()
        {

            public void Notify(String charset)
            {
                found = true;
                result = charset;
            }
        });
        BufferedInputStream imp = new BufferedInputStream(in);
        byte[] buf = new byte[1024];
        int len;
        boolean isAscii = true;
        while ((len = imp.read(buf, 0, buf.length)) != -1)
        {
            // Check if the stream is only ascii.
            if (isAscii)
                isAscii = det.isAscii(buf, len);
            // DoIt if non-ascii and not done yet.
            if (!isAscii)
            {
                if (det.DoIt(buf, len, false))
                    break;
            }
        }
        imp.close();
        in.close();
        det.DataEnd();
        if (isAscii)
        {
            found = true;
            prob = new String[]
                    {
                        "ASCII"
                    };
        } else if (found)
        {
            prob = new String[]
                    {
                        result
                    };
        } else
        {
            prob = det.getProbableCharsets();
        }
        return prob;
    }

    public String[] detectAllCharset(InputStream in) throws IOException
    {
        try
        {
            lang = nsPSMDetector.ALL;
            return detectChineseCharset(in);
        } catch (IOException e)
        {
            throw e;
        }
    }
}

 

 

Demo:这个演示CharsetDetector用法示例

 

package cn.xddai.chardet;

import java.io.IOException;
import java.net.URL;

/**
 *
 * @author xddai
 */
public class Demo
{
    public static void main(String[] args)throws IOException
    {
        CharsetDetector charDect = new CharsetDetector();
        URL url = new URL("http://www.qq.com/");
        String[] probableSet = charDect.detectChineseCharset(url.openStream());
        for (String charset : probableSet)
        {
            System.out.println(charset);
        }
    }
}
 封装后的jar包下载地址http://codeinplatform.googlecode.com/files/CharsetDetector.jar

最后,感谢Mozilla大神为开源做的贡献。

分享到:
评论
1 楼 vFISHv 2011-03-30  
detectAllCharset  这个不对吧  ...... 

相关推荐

    java使用jchardet检测文本文件(字节流)的编码方式

    NULL 博文链接:https://zhifeiji512.iteye.com/blog/1221068

    jchardet jchardet

    jchardet-1.1 字符编码识别jchardet-1.1 字符编码识jchardet是mozilla自动字Java字符串(及字符)类以Unicode编码保存数据。当处理来自外部的国际性文本时,我们需要提供关于这些文本的编码,以便准确地将它们转换为...

    jchardet-1.1jchardet-1.1jchardet-1.1

    jchardet-1.1 字符编码识别jchardet-1.1 字符编码识jchardet是mozilla自动字Java字符串(及字符)类以Unicode编码保存数据。当处理来自外部的国际性文本时,我们需要提供关于这些文本的编码,以便准确地将它们转换为...

    jchardet-1.0.jar

    jchardet-1.0.jar java判断文件字符编码的类库!

    jchardet-1.1

    一个 mozilla 的 编码检测 java 包.

    jchardet.jar chardet.jar jchardet-1.1.zip

    jchardet-1.1.zip jchardet-1.1.zip 包含源码及jar。 jchardet-1.1.zip

    JChardet jar包,封装类.rar

    鱼泽弋海 封装的Jchardet类以及chardet.jar,感觉是网上找到的例子里写的很好的。 Jchardet,会返回一个最有可能的charset,和一组其他可能,正确率还行,但在数据很少的情况下,出错率会变高,可以用RadioGroup...

    C# port of Mozilla charset detector

    作用:检测文本文件的编码方式,例如GB2312、UTF-8、Shift-jis、ISO-8859-1等等。相关讨论可以参考http://blog.csdn.net/wingtrace/archive/2007/03/17/1532262.aspx 。 用法:请参考GenericCharsetDetector类的几...

    批量文件编码字符集工具

    一个用来检查文件编码字符集的工具,使用jchardet作为编码检测的组件!

    rh-maven33-jchardet-1.1-12.4.el7.noarch.rpm

    官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装

    使用cpdetector 三方库读取文件编码

    使用cpdetector 三方库读取文件编码。 jar包和事例代码都有。

    detector探测器

    上面代码中的detector不仅可以用于探测文件的编码,也可以探测任意输入的文本流的编码,方法是调用其重载形式: Java代码 charset=detector.detectCodepage(待测的文本输入流,测量该流所需的读入字节数);

    rh-maven33-jchardet-javadoc-1.1-12.4.el7.noarch.rpm

    官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装

    文本乱码识别工具包下载

    爬虫中经常遇到采集网页乱码问题,此时程序根据meta、header无法识别情况下,下载需要的包 antlr.jar、cpdetector-1.0.10.jar、jchardet-1.0.jar

Global site tag (gtag.js) - Google Analytics