Read a Unicode file

Problem:

You want to read a Unicode encoded file into memory.

Solution:

To read a file with a charset other than the system default you should specify the charset in the reader.  You can specify either a Charset or the String id of the Charset.

To read a Unicode file containing Japanese characters:

//Surround with try catch to handle potential exception
try{
    //Get an InputStream
    FileInputStream fis = new FileInputStream("C:\\files\\test.txt");
    //Get a reader specifying the charset
    InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
    //wrap with a buffered reader for performance
    BufferedReader br = new BufferedReader(isr);
    //Read it into a variable an output
    String txt;
    while((txt = br.readLine()) != null){
        System.out.println(txt);
    }
}catch (FileNotFoundException e){
    e.printStackTrace();
}catch (IOException e){
    e.printStackTrace();
}

 


The output:

これはテストです。
高松
日本
米国
英国
世界