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