トップ 一覧 ping 検索 ヘルプ RSS ログイン

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

  • 追加された行はこのように表示されます。
  • 削除された行はこのように表示されます。
!!!Java ファイルの文字コードを判別する
[Java]{{category 文字化け}}

!!JISAutoDetect
*http://d.hatena.ne.jp/onozaty/20051214/p1

!!juniversalchardet
*http://code.google.com/p/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;
 }