Java Examples - Read a file

Advertisements


Problem Description:

How to read a file?

Solution:

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

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) {
         }
      }
   }
}

Result:

The above code sample will produce the following result.

aString


Advertisements
Advertisements