| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

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

提供: MyMemoWiki
ナビゲーションに移動 検索に移動

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

Java |

JISAutoDetect

juniversalchardet

  1. public static String detectFileEncoding(File file) throws IOException {
  2. String result = null;
  3. byte[] buf = new byte[4096];
  4. FileInputStream fis = new FileInputStream(file);
  5. UniversalDetector detector = new UniversalDetector(null);
  6. int nread;
  7. while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {
  8. detector.handleData(buf, 0, nread);
  9. }
  10. detector.dataEnd();
  11. result = detector.getDetectedCharset();
  12. detector.reset();
  13. return result;
  14. }