Java获得指定文件的编码类型
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;import info.monitorenter.cpdetector.io.JChardetFacade;import java.io.File;import java.nio.charset.Charset;/** * 获得文件的编码类型 * 需要三个jar包:antlr.jar,chardet.jar,cpdetector_1.0.7.jar * */public class CharacterEnding {public static void main(String[] args) {new CharacterEnding().getFileCharacterEnding("C://temps//zip压缩");}/** * 获得文件的编码类型 * @param filePath * @return */public String getFileCharacterEnding(String filePath ) {String fileCharacterEnding = "UTF-8";CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();detector.add(JChardetFacade.getInstance());Charset charset = null;File file = new File(filePath);try {charset = detector.detectCodepage(file.toURL());} catch (Exception e) {e.printStackTrace();}if (charset != null) {fileCharacterEnding = charset.name();System.out.println(filePath+"的编码类型为:"+fileCharacterEnding);}return fileCharacterEnding;}}
页:
[1]