- package test;
- //import cpdetector.io.*;
- import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
- import info.monitorenter.cpdetector.io.JChardetFacade;
- import java.io.*;
- import java.nio.charset.*;
- public class PageCode {
- public static void main(String args[]) {
- CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
- detector.add(JChardetFacade.getInstance());
- Charset charset = null;
- File f = new File("D:/test/1.xls");
- try {
- charset = detector.detectCodepage(f.toURL());
- } catch (Exception e) {
- e.printStackTrace();
- }
- if (charset != null) {
- System.out.println(f.getName() + "编码是:" + charset.name());
- } else
- System.out.println(f.getName() + "未知");
- }
- }
原理:
- import java.io.*;
- public class TestText {
- public static void main(String args[]){
- File file = new File("C:/1.txt");
- try{
- InputStream stream = new FileInputStream(file);
- byte[] byteArray = new byte[3];
- stream.read(byteArray);
- stream.close();
- if (byteArray[0] == -17 && byteArray[1] == -69 && byteArray[2] == -65)
- System.out.println("UTF-8");
- else
- System.out.println("可能是其他的编码");
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- }
http://blog.csdn.net/justinavril/archive/2008/08/07/2783266.aspx
jar包下载地址:
http://ncu.dl.sourceforge.net/project/cpdetector/cpdetector/binaries/cpdetector_1.0.7_binary.zip