Core Java Tutorial

Core Java Tutorial

How to Reading a file using Bufferedreader

BufferedReader

Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.

Example:

 

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;


public class BufferedReaderExample {


 public static void main(String[] args) {


  File f1 = new File("sam.txt");

  try {

   String s;

   FileReader fr = new FileReader(f1);

   BufferedReader br = new BufferedReader(fr);

   while ((s = br.readLine()) != null)

   {

    System.out.println(s);

   }

   br.close();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }


 }

}
Make sure you have sam.txt file in current directory. Run the program and check the output.