Java I/O How to - Read UTF16 file








Question

We would like to know how to read UTF16 file.

Answer

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
//w w w. j ava 2s . c o m
public class Main {

  public static void main(final String[] args) throws IOException {
    File infile = new File("/tmp/utf16.txt");
    FileInputStream inputStream = new FileInputStream(infile);
    Reader in = new InputStreamReader(inputStream, "UTF-16");
    int read;
    while ((read = in.read()) != -1) {
      System.out.print(Character.toChars(read));
    }
    in.close();
  }
}