JAVA判断文件编码类型
java读取文件,处理过程中,可能因为文件的编码问题导致了中文乱码。有时需要将UTF-8的改为ANSI的编码。以下代码就可以判断文件是什么编码方式。
主要jar包:cpdetector.jar
下载地址http://cpdetector.sourceforge.net/
同时还需jchardet-1.0.jar这个包,否则detector.add(cpdetector.io.JChardetFacade.getInstance()); 会报错;
下载地址http://www.jarfinder.com/index.php/jars/versionInfo/40297
还有一个antlr.jar,不然运行过程中detector.add(new ParsingDetector(false));会报错;
下载地址http://www.java2s.com/Code/Jar/ABC/Downloadantlrjar.htm
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.nio.charset.Charset;/** * *//** * @author weict * */public class JudgeFileCode {/** * @param args */public static void main(String[] args) {/*------------------------------------ detector是探测器,它把探测任务交给具体的探测实现类的实例完成。 cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法 加进来,如ParsingDetector、 JChardetFacade、ASCIIDetector、UnicodeDetector。 detector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的 字符集编码。 --------------------------------------*/CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();/*------------------------------------- ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于 指示是否显示探测过程的详细信息,为false不显示。 ---------------------------------------*/detector.add(new ParsingDetector(false));//如果不希望判断xml的encoding,而是要判断该xml文件的编码,则可以注释掉/*-------------------------------------- JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码 测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以 再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。 ---------------------------------------*/detector.add(JChardetFacade.getInstance());// ASCIIDetector用于ASCII编码测定detector.add(ASCIIDetector.getInstance());// UnicodeDetector用于Unicode家族编码的测定detector.add(UnicodeDetector.getInstance());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() + "未知");}}}charset=detector.detectCodepage(待测的文本输入流,测量该流所需的读入字节数);
FileInputStream ios=new FileInputStream("属性文件名"); Properties prop=new Properties(); prop.load(ios); ios.close(); String value=prop.getProperty("属性名"); String encValue=new String(value.getBytes("iso-8859-1"),"属性文件的实际编码");