Sunday, January 6, 2013

Print content of a text file

Source:
import java.io.*;
class PrintFile {
   public static void main (String[] args) {
      File source = new File ("MyFile.txt");
      FileReader fr = null;
      try {
         fr = new FileReader (source);
         int x;
         while ( (x = fr.read()) != -1 ) {
            System.out.printf ("%c", x);
         }
      } catch (Exception e) {
      } 
   }
}

Output:
   # java PrintFile
   This is line one.
   This is line two.
   This is line three.
   This is line four.