JAVA检测文件字符编码

阅读更多
    JAR:   http://sourceforge.net/projects/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;
import java.io.File;
import java.nio.charset.Charset;
/**
 * @author   ---
 * @version  1.0, 2011-12-20
 * @since    JDK1.5
 */
public class FileEncodeDetector {	
	/**
	 * @param file
	 * @return
	 */
	public static String charsetDetect(File file) {
		CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
		detector.add(new ParsingDetector(false));
		detector.add(JChardetFacade.getInstance());
		detector.add(ASCIIDetector.getInstance());
		detector.add(UnicodeDetector.getInstance());	
		String encode = null;
		Charset charset = null;
		try {
			charset = detector.detectCodepage(file.toURI().toURL());	
			if (charset != null) {
				encode = charset.name();
			} else {
				// ...
			}
		} catch (Exception ex) {
			// ...
		}
		return encode;
	}
}
 

你可能感兴趣的:(JAVA检测文件字符编码)