Reading a file that is encoded in UTF-8 using a Reader

You can specify the encoding UTF-8 with InputStreamReader. This class will convert the incoming bytes to Unicode.

Main.java:

import java.io.*;
 
public class Main {
   public static void main(String args[]) {
      if (args.length != 1) {
         System.out.println("Usage: java Main <utf8file>");
         System.exit(1);
      }
    
      try {
         InputStreamReader isr = new InputStreamReader(new FileInputStream(args[0]), "UTF8");
         BufferedReader br = new BufferedReader(isr);
         String line;
         while ((line = br.readLine()) != null) {
            System.out.println(line);
         }
         br.close();
      }
      catch(FileNotFoundException e) {
         System.out.println(e);
      }
      catch(IOException e) {
         System.out.println(e);
      }
   }
}

Note: Since UTF-8 is backwards compatible with ASCII, you can test this program with those types of files.