「Java ファイルの文字コードを判別する」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「==Java ファイルの文字コードを判別する== [Java]{{category 文字化け}} ===JISAutoDetect=== *http://d.hatena.ne.jp/onozaty/20051214/p1 ===juniversa…」) |
|||
| 1行目: | 1行目: | ||
==Java ファイルの文字コードを判別する== | ==Java ファイルの文字コードを判別する== | ||
| − | [Java]{{category 文字化け}} | + | [[Java]]{{category 文字化け}} |
===JISAutoDetect=== | ===JISAutoDetect=== | ||
| 15行目: | 15行目: | ||
int nread; | int nread; | ||
| − | while ((nread = fis.read(buf)) | + | while ((nread = fis.read(buf)) > 0 && !detector.isDone()) { |
detector.handleData(buf, 0, nread); | detector.handleData(buf, 0, nread); | ||
} | } | ||
2020年2月15日 (土) 08:03時点における版
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 矢木浩人