Signup/Sign In

Java BufferedReader Class

In this tutorial, we will learn about BufferedReader class in Java. This class reads a text from a character input stream. It stores the character in a buffer while reading so that this process of reading becomes more efficient. We can specify the buffer size, otherwise, it will take the default size.

Syntax

This is the syntax declaration of BufferedReader class snd this class extends the Reader class.

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

All the methods supported by BufferedReader class are given below.

Method Description
int read() This method is used for reading a single character.
int read(char[] cbuf, int off, int len) This method is used for reading characters into a portion of an array.
boolean markSupported() This method is used to test the input stream support for the mark and reset method.
String readLine() This method is used for reading a line of text.
boolean ready() This method is used to test whether the input stream is ready to be read.
long skip(long n) This method is used for skipping the characters.
void reset() This method repositions the stream at a position the mark method was last called on this input stream.
void mark(int readAheadLimit) This method is used for marking the present position in a stream.
void close() This method closes the input stream and releases any of the system resources associated with the stream.

Example of BufferedReader

In this example, we are using the read() method to read characters from a stream. This method reads a single character at a time if the end of the stream reaches then it will return -1. Here we will read from the stream until it reaches the end we are reading characters one by one and printing on the console as we reach the end of the stream we stop the loop.

package studytonight;
import java.io.BufferedReader;
import java.io.FileReader;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			FileReader fileReader=new FileReader("E:\\studytonight\\output.txt");    
			BufferedReader br=new BufferedReader(fileReader);    
			int i;    
			while((i=br.read())!=-1){  
				System.out.print((char)i);  
			}  
			br.close();    
			fileReader.close();    
		}
		catch(Exception e)
		{
			System.out.print(false);
		}
	}
}


This is a text.

output.txt

This is a text.

Example of readLine() method in BufferedReader

In this example, we are implementing the readLine() method, this method reads a line of text. Here the line is considered until '\n' or '\r' occurred. It will return a null value if we reach the end of a stream.

package studytonight;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
			System.out.println("Enter your name: ");
			String name = reader.readLine();   
			System.out.println("Hello "+name);
		}
		catch(Exception e)
		{
			System.out.print(false);
		}
	}
}


Enter your name:
Java
Hello Java

Conclusion

In this tutorial, we learned about BufferedReader class in Java. This class reads a text from a character input stream. It stores the character in a buffer while reading so that this process of reading becomes more efficient. The BufferedReader class maintains an internal buffer of 8192 characters.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.