Monday, April 25, 2011

Java read file as utf8

Specify "UTF-8" as charsetName to the constructor of InputStreamReader. From JavaDoc:

An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.
BufferedReader r = new BufferedReader(
   new InputStreamReader(
       new FileInputStream("com/demo/io/test.txt"), 
       "UTF-8"
   )
     );
String l = r.readLine();
while (l != null) {
    System.out.println(l);
    l = r.readLine();
}
r.close();

No comments:

Post a Comment

Chitika