How to read a file?


Levels of difficulty: / perform operation:

Problem & Solution

This example shows how to read a file using readLine method of BufferedReader class.

JAVA Program

import java.io.*;

public class Main {
   public static void main(String[] args)  {
      try {
         BufferedReader in = new BufferedReader
         (new FileReader("c:\\filename"));
         String str;
         while ((str = in.readLine()) != null) {
            System.out.println(str);
         }
         System.out.println(str);
         }
         catch (IOException e) {
         }
      }
   }
}

Output

The above code sample will produce the following result.

aString