トップ 差分 一覧 ping ソース 検索 ヘルプ PDF RSS ログイン

Java ファイルの文字コードを判別する



目次



記事一覧

キーワード

Java ファイルの文字コードを判別する

[Java][文字化け]

 JISAutoDetect


 juniversalchardet


public static String detectFileEncoding(File file) throws IOException  {
    String result = null;
    byte[] buf = new byte[4096];
    FileInputStream fis = new FileInputStream(file);
    UniversalDetector detector = new UniversalDetector(null);
    
    int nread;
    while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {
        detector.handleData(buf, 0, nread);
    }
    detector.dataEnd();
    
    result =  detector.getDetectedCharset();
    detector.reset();
    
    return result;
}



YAGI Hiroto (piroto@a-net.email.ne.jp)
twitter http://twitter.com/pppiroto

Copyright© 矢木 浩人 All Rights Reserved.