您的位置:首页 > 移动开发 > Android开发

android 动态设置读取文件的编码格式(charset)

2014-03-24 16:57 986 查看
android中使用InputStreamReader读取文件数据时,需要传入参数编码格式(charset),而当目标文件的编码格式不固定时,就需要动态获取编码格式。

InputStreamReader inputReader = new InputStreamReader(inputStream, charset);//charset可以设置为"GBK',"UTF-8"

以下主要描述通过文件编码探测器获取charset。

1,简单的编码类型判断,只能识别GBK和UTF-8,通过读取文件头的3个字符

public String charsetDetect(String path) {

String _charset="";

try {

File file = new File(path);

InputStream fs = new FileInputStream(file);

byte[] buffer = new byte[3];

fs.read(buffer);

fs.close();

if (buffer[0] == -17 && buffer[1] == -69 && buffer[2] == -65)

_charset="UTF-8";

else

_charset="GBK";

}

catch (IOException e) {e.printStackTrace(); }

return _charset;

}

2,更加安全的文件编码检测,可以使用一个开源项目cpdetector,可使用这个网址下载:http://sourceforge.jp/projects/sfnet_cpdetector/

2.1 将下载的文件解压会找jar文件:cpdetector_1.0.10.jar,antlr-2.7.4.jar,chardet-1.0.jar,jargs-1.0.jar,将jar文件放到工程的libs文件夹中。

在工程的树形目录中,找到工程文件节点位置点击右键,在弹出菜单中选择"Build Path"->"Add External Archives..",把cpdetector_1.0.10.jar添加进工程中。

2.2 在需要检测编码类型的文件中添加如下代码进行测试。

//用于支持文件读写

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

//用于支持cpdetector;

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;

/**

* 读取文件的编码格式,返回文件文件编码类型

*/

public String charsetDetect(String path)

{

String _charset="UTF-8";//默认

CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();

detector.add(new ParsingDetector(false));

detector.add(JChardetFacade.getInstance());//用到antlr.jar、chardet.jar

detector.add(ASCIIDetector.getInstance());

detector.add(UnicodeDetector.getInstance());

java.nio.charset.Charset set = null;

File f = new File(path);

try {

set = detector.detectCodepage(f.toURI().toURL());

} catch (Exception ex) {

ex.printStackTrace();

}

if (set != null)

_charset = set.name();

return _charset;

}

2.3关于cpdetector的一些函数介绍

/*------------------------------------------------------------------------

detector是探测器,它把探测任务交给具体的探测实现类的实例完成。

cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法

加进来,如ParsingDetector、 JChardetFacade、ASCIIDetector、UnicodeDetector。

detector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的

字符集编码。

--------------------------------------------------------------------------*/

cpdetector.io.CodepageDetectorProxy detector =

cpdetector.io.CodepageDetectorProxy.getInstance();

/*-------------------------------------------------------------------------

ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于

指示是否显示探测过程的详细信息,为false不显示。

---------------------------------------------------------------------------*/

detector.add(new cpdetector.io.ParsingDetector(false));

/*--------------------------------------------------------------------------

JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码

测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以

再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。

---------------------------------------------------------------------------*/

detector.add(cpdetector.io.JChardetFacade.getInstance());

//ASCIIDetector用于ASCII编码测定

detector.add(cpdetector.io.ASCIIDetector.getInstance());

//UnicodeDetector用于Unicode家族编码的测定

detector.add(cpdetector.io.UnicodeDetector.getInstance());

JAVA.nio.charset.Charset charset = null;

File f=new File("待测的文本文件名");

try {

charset = detector.detectCodepage(f.toURL());

} catch (Exception ex) {ex.printStackTrace();}

if(charset!=null){

System.out.println(f.getName()+"编码是:"+charset.name());

}else

System.out.println(f.getName()+"未知");

/*------------------------------------------------------------------------

detector是探测器,它把探测任务交给具体的探测实现类的实例完成。

cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法

加进来,如ParsingDetector、 JChardetFacade、ASCIIDetector、UnicodeDetector。

detector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的

字符集编码。

--------------------------------------------------------------------------*/

cpdetector.io.CodepageDetectorProxy detector =

cpdetector.io.CodepageDetectorProxy.getInstance();

/*-------------------------------------------------------------------------

ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于

指示是否显示探测过程的详细信息,为false不显示。

---------------------------------------------------------------------------*/

detector.add(new cpdetector.io.ParsingDetector(false));

/*--------------------------------------------------------------------------

JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码

测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以

再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。

---------------------------------------------------------------------------*/

detector.add(cpdetector.io.JChardetFacade.getInstance());

//ASCIIDetector用于ASCII编码测定

detector.add(cpdetector.io.ASCIIDetector.getInstance());

//UnicodeDetector用于Unicode家族编码的测定

detector.add(cpdetector.io.UnicodeDetector.getInstance());

JAVA.nio.charset.Charset charset = null;

File f=new File("待测的文本文件名");

try {

charset = detector.detectCodepage(f.toURL());

} catch (Exception ex) {ex.printStackTrace();}

if(charset!=null){

System.out.println(f.getName()+"编码是:"+charset.name());

}else

System.out.println(f.getName()+"未知");

上面代码中的detector不仅可以用于探测文件的编码,也可以探测任意输入的文本流的编码,方法是调用其重载形式:

Java代码

charset=detector.detectCodepage(待测的文本输入流,测量该流所需的读入字节数);

charset=detector.detectCodepage(待测的文本输入流,测量该流所需的读入字节数);

上面的字节数由程序员指定,字节数越多,判定越准确,当然时间也花得越长。要注意,字节数的指定不能超过文本流的最大长度。

判定文件编码的具体应用举例:

属性文件(.properties)是Java程序中的常用文本存储方式,象STRUTS框架就是利用属性文件存储程序中的字符串资源。它的内容如下所示:

Java代码

#注释语句

属性名=属性值

#注释语句

属性名=属性值

读入属性文件的一般方法是:

Java代码

FileInputStream ios=new FileInputStream("属性文件名");

Properties prop=new Properties();

prop.load(ios);

ios.close();

FileInputStream ios=new FileInputStream("属性文件名");

Properties prop=new Properties();

prop.load(ios);

ios.close();

利用java.io.Properties的load方法读入属性文件虽然方便,但如果属性文件中有中文,在读入之后就会发现出现乱码现象。发生这个原因是load方法使用字节流读入文本,在读入后需要将字节流编码成为字符串,而它使用的编码是“iso-8859-1”,这个字符集是ASCII码字符集,不支持中文编码,所以这时需要使用显式的转码:

Java代码

String value=prop.getProperty("属性名");

String encValue=new String(value.getBytes("iso-8859-1"),"属性文件的实际编码");

String value=prop.getProperty("属性名");

String encValue=new String(value.getBytes("iso-8859-1"),"属性文件的实际编码");

在上面的代码中,属性文件的实际编码就可以利用上面的方法获得。当然,象这种属性文件是项目内部的,我们可以控制属性文件的编码格式,比如约定采用Windows内定的GBK,就直接利用"gbk"来转码,如果约定采用UTF-8,也可以是使用"UTF-8"直接转码。如果想灵活一些,做到自动探测编码,就可利用上面介绍的方法测定属性文件的编码,从而方便开发人员的工作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: