Java I/O How to - Create a Reader from File Name








Question

We would like to know how to create a Reader from File Name.

Answer

 //from   w w  w.  j  a v a2  s  .c o m


import java.io.FileReader;

public class Main {
  public static void main(String[] argv) throws Exception {
    FileReader fr = new FileReader("text.txt");
    int count;
    char chrs[] = new char[80];

    do {
      count = fr.read(chrs);
      for (int i = 0; i < count; i++)
        System.out.print(chrs[i]);
    } while (count != -1);

  }
}