「Java ファイルの文字コードを判別する」の版間の差分
ナビゲーションに移動
検索に移動
| 1行目: | 1行目: | ||
==Java ファイルの文字コードを判別する== | ==Java ファイルの文字コードを判別する== | ||
| − | [[Java]]{{category 文字化け}} | + | [[Java]] | {{category 文字化け}} |
===JISAutoDetect=== | ===JISAutoDetect=== | ||
2020年2月15日 (土) 08:36時点における版
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;
}
© 2006 矢木浩人