- 読み込む文書( BOM付きUTF-8 )
123456789
abcdefghi
あいうえお
- 読み込みプログラム
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class UTF8TextExam {
/**
* @param args
*/
public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
ClassLoader.getSystemResourceAsStream("UTF8.txt"), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
for (int cnt = 0; cnt < line.length(); cnt++) {
char ch = line.charAt(cnt);
System.out.print("[" + ch + "]:" + toCharCode(ch));
}
System.out.print("\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String toCharCode(char ch) {
String ret = "0000" + Integer.toHexString(ch & 0xffff);
return ret.substring(ret.length() - 4);
}
}
- 読み込み結果
[?]:feff[1]:0031[2]:0032[3]:0033[4]:0034[5]:0035[6]:0036[7]:0037[8]:0038
[9]:0039[a]:0061[b]:0062[c]:0063[d]:0064[e]:0065[f]:0066[g]:0067[h]:0068
[i]:0069[あ]:3042[い]:3044[う]:3046[え]:3048[お]:304a
最初の一文字目が BOM 文字になっている