cpdetector_1.0.10_binary.zip · 王雨龙/Web - Gitee.com
import info.monitorenter.cpdetector.io.*;
import java.io.*;
import java.nio.charset.Charset;
public class autoEnCode {
public static void main(String[] args) throws Exception {
System.out.println(getFileEncode("1.txt"));
}
/**
*
* @param filePath
* @return
*/
public static String getFileEncode(String filePath) {
String charsetName = null;
try {
File file = new File(filePath);
CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
detector.add(new ParsingDetector(false));
detector.add(JChardetFacade.getInstance());
detector.add(ASCIIDetector.getInstance());
detector.add(UnicodeDetector.getInstance());
Charset charset = null;
charset = detector.detectCodepage(file.toURI().toURL());
if (charset != null) {
charsetName = charset.name();
} else {
charsetName = "UTF-8";
}
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
return charsetName;
}
}