commons-file/src/main/java/com/xiongyingqi/util/FileEncode.java
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;
    }
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;
    }
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 ; }
使用工具获取文件的编码方式

@param file
@return
使用工具获取文件的编码方式