Buffered Reader in Java

br1

Java provides several mechanism to read Files. The most useful package that is provided for this is thejava.io.Reader. This class contains the Class BufferedReader under package java.io.BufferedReader

Let us see what this class has in store for us.

This class reads the text from an Input stream (like file) by buffering characters that efficiently reads characters, arrays or lines.

In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders.

A typical usage would involve passing the file path to the BufferedReader as follows:

The code will look like: (Assuming you have a text file in D drive)

objReader = new BufferedReader(new FileReader("D:DukesDiary.txt"));

This basically loads your file in the objReader. Now, you will need to iterate through the contents of the file and print it. The while loop in the below code will read the file until it has reached the end of file i. e. there is no contents in the objReader.readLine() – that basically returns a string. Hence, the loop will iterate until it’s not null.

The below code shows the complete implementation.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadFileExample {

	public static void main(String[] args) {

		BufferedReader objReader = null;

		try {
			String strCurrentLine;

			objReader = new BufferedReader(new FileReader("D:DukesDiary.txt"));

			while ((strCurrentLine = objReader.readLine()) != null) {

				System.out.println(strCurrentLine);
			}

		} catch (IOException e) {

			e.printStackTrace();

		} finally {

			try {
				if (objReader != null)
					objReader.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}
}

Note:

The above code has some very important handlings especially in the finally block of the code. This code will ensure that the memory management is done efficiently and the objReader.close() method is called that releases the memory.

6 Comments
  1. Very good tutorial…

  2. Hello,

    I have one small doubt , does this code read Microsoft Office suit file format data such as .doc , .docx etc ?

    • It is not possible to read .docx file using bufferreader class. It is possible by org.apache.poi.hwpf.HWPFDocument and org.apache.poi.hwpf.extractor.WordExtractor

  3. Hi,

    I am new to java. Pls help me understand this. Why do you check as objreader != Null. Shouldn’t it be checking if objreader equal to null to close the file ?
    Thanks,
    Aaron.

    • objreader != Null
      means file is open and should be closed
      objreader = Null
      means no file open, then what is the point in closing it.

  4. Hi AARON,
    I am not pro in java but i am guessing that if objreader is null then it will not going to affect precious memory of JVM.that’s y no need to close it.
    @author—> Awesome tutorial in very simplified manner explained.thanks for the post.

Leave a Reply