• We can use BufferedReader to Read Character Data(text data) from file.
  • The main advantage of BufferedReader over FileReader is we can Read Data Line by line in addition to Character by Character, which is more convenient to the programmer.

Constructors :

BufferedReader br = new BufferedReader(Reader r);
BufferedReader br = new BufferedReader(Reader r, int bufferSize);

Note : BufferedReader cannot directly communicate with the file, it has to communicate via some reader object.

Methods :

int read();
int read(char[] ch);
void close();
String readLine();
  • It attempts to read next line from the file and returns it, if it is available.
  • It next line is not available, then it will return null.

Sample program to read line using buffered reader :

package com.fileDemo;

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

public class Demo {
	public static void main(String[] args) throws IOException {
		File f = new File("/home/tyson/Desktop/temp/file2");
		FileReader fr = new FileReader(f);
		BufferedReader br = new BufferedReader(fr);
		String line = br.readLine();
		while(line!=null) {
			line = br.readLine();

Note :

  • We need not close FileReader object, while closing BufferedReader object the FileReader object is closed implicitly.
  • Most enhanced reader to read data from the file is BufferedReader

Leave a Comment