BufferedReader Class

BufferedReader Class

In this tutorial, we are going to discuss about BufferedReader Class in java. By using this class we can read character data from the file.

The BufferedReader class in Java, found in the java.io package, is used to read character input from a reader, such as a FileReader, with buffering for improved performance. It reads text from a character-input stream, buffering characters to provide efficient reading of characters, arrays, and lines.

BufferedReader Class
Constructors
BufferedReader br = new BufferedReader(Reader r);
BufferedReader br = new BufferedReader(Reader r, int buffersize);

BufferedReader never communicates directly with the file. It should Communicate through some reader object only.

Important methods
int read()
int read(char [] ch)
String readLine(); 
void close()

E.g

package com.ashok.files;

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

public class MyFile {
   public static void main(String arg[]) throws Exception {
      FileReader fr = new FileReader("ashok.txt");
      BufferedReader br = new BufferedReader(fr);
      String s = br.readLine();
      while (s != null) {
         System.out.println(s);
         s = br.readLine();
      }
      br.close();
   }
}

Output

a
abcd
Ashok
Waytoeasylearn

Note

When ever we are closing BufferedReader, automatically underlying FileReader object will be closed.

That’s all about the BufferedReader Class in java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!

BufferedReader Class
Scroll to top