JavaBufferedReader

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java BufferedReader Class

Java BufferedReader class is used to read the text from a character-based input stream. It can be used to read data line by line by readLine() method. It makes the performance fast. It inherits Reader class.

Java BufferedReader class declaration

Let's see the declaration for Java.io.BufferedReader class:

  1. public class BufferedReader extends Reader  

Java BufferedReader class constructors

Constructor

Description

BufferedReader(Reader rd)

It is used to create a buffered character input stream that uses the default size for an input buffer.

BufferedReader(Reader rd, int size)

It is used to create a buffered character input stream that uses the specified size for an input buffer.

Java BufferedReader class methods

Method

Description

int read()

It is used for reading a single character.

int read(char[] cbuf, int off, int len)

It is used for reading characters into a portion of an array.

boolean markSupported()

It is used to test the input stream support for the mark and reset method.

String readLine()

It is used for reading a line of text.

boolean ready()

It is used to test whether the input stream is ready to be read.

long skip(long n)

It is used for skipping the characters.

void reset()

It repositions the stream at a position the mark method was last called on this input stream.

void mark(int readAheadLimit)

It is used for marking the present position in a stream.

void close()

It closes the input stream and releases any of the system resources associated with the stream.

Java BufferedReader Example

In this example, we are reading the data from the text file testout.txt using Java BufferedReader class.

  1. package com.javatpoint;  
  2. import java.io.*;  
  3. public class BufferedReaderExample {  
  4.     public static void main(String args[])throws Exception{    
  5.           FileReader fr=new FileReader("D:\testout.txt");    
  6.           BufferedReader br=new BufferedReader(fr);    
  7.   
  8.           int i;    
  9.           while((i=br.read())!=-1){  
  10.           System.out.print((char)i);  
  11.           }  
  12.           br.close();    
  13.           fr.close();    
  14.     }    
  15. }    

Here, we are assuming that you have following data in "testout.txt" file:

Welcome to tutorialtours.

Output:

Welcome to tutorialtours.

Reading data from console by InputStreamReader and BufferedReader

In this example, we are connecting the BufferedReader stream with the InputStreamReader stream for reading the line by line data from the keyboard.

  1. package com.javatpoint;  
  2. import java.io.*;  
  3. public class BufferedReaderExample{    
  4. public static void main(String args[])throws Exception{             
  5.     InputStreamReader r=new InputStreamReader(System.in);    
  6.     BufferedReader br=new BufferedReader(r);            
  7.     System.out.println("Enter your name");    
  8.     String name=br.readLine();    
  9.     System.out.println("Welcome "+name);    
  10. }    
  11. }  

Output:

Enter your name
Nakul Jain
Welcome Nakul Jain

Java Buffer reader class 1

Another example of reading data from console until user writes stop

In this example, we are reading and printing the data until the user prints stop.

  1. package com.javatpoint;  
  2. import java.io.*;  
  3. public class BufferedReaderExample{    
  4. public static void main(String args[])throws Exception{             
  5.      InputStreamReader r=new InputStreamReader(System.in);    
  6.      BufferedReader br=new BufferedReader(r);           
  7.      String name="";    
  8.      while(!name.equals("stop")){    
  9.       System.out.println("Enter data: ");    
  10.       name=br.readLine();    
  11.       System.out.println("data is: "+name);    
  12.      }              
  13.     br.close();    
  14.     r.close();    
  15.     }    
  16.     }  

Output:

Enter data: Nakul
data is: Nakul
Enter data: 12
data is: 12
Enter data: stop
data is: stop