com.xiongyingqi.util.FileEncode Maven / Gradle / Ivy
package com.xiongyingqi.util;
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 java.io.File;
import java.nio.charset.Charset;
/**
* Created by 瑛琪xiongyingqi.com on 2014/9/23 0023.
*/
public class FileEncode {
/**
* 使用工具获取文件的编码方式
*
* @param file
* @return
*/
public static synchronized String getEncodeByUtil(File file) {
CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
detector.add(new ParsingDetector(false));
detector.add(JChardetFacade.getInstance());// 用到antlr.jar、chardet.jar
// ASCIIDetector用于ASCII编码测定
detector.add(ASCIIDetector.getInstance());
// UnicodeDetector用于Unicode家族编码的测定
// detector.add(UnicodeDetector.getInstance());
Charset charset = null;
try {
charset = detector.detectCodepage(file.toURI().toURL());
} catch (Exception ex) {
ex.printStackTrace();
}
if (charset != null)
return charset.name();
else
return null;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy