BufferedReader Class

BufferedReader

By using this class we can read character data from the file.

Constructors
1. BufferedReader br = new BufferedReader(Reader r)
2. 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
1. int read()
2. int read(char [] ch)
3. String readLine(); 
Reads the next line present in the file. If there is no nextline this method returns null.
4. 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.

BufferedReader Class

Scroll to top