Javatpoint Logo
Javatpoint Logo

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:


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.

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

Welcome to javaTpoint.

Output:

Welcome to javaTpoint.

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.

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.

Output:

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




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA