Skip to main content

How do I read a text file with non system default character set

In this Q&A, we'll find out how easy it is to ready from a text file with non system default character set

Java uses a default Charset encoding to convert the byte stream to a string.  You can find out the default character set with the code below:
System.out.println( Charset.defaultCharset().displayName() );

To use a charset that's not a system default, one will have to specify the charset when creating the InputStreamReader as below:
File f = new File("c:/users/hari/test.txt");
// Specify the charset when creating the InputStreamReader object
InputStreamReader is = new InputStreamReader(new FileInputStream(f), Charset.forName("UTF8") );
BufferedReader bf = new BufferedReader( is );
bf.lines().forEach(System.out::println);

Comments