Java BufferedReader Examples

BufferedReader class present in the java.io package. It implements Closeable, AutoCloseable, Readable interfaces.

It is used to Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. 

 It is present in java since JDK1.1.
Systax BufferedReader in = new BufferedReader(new FileReader("foo.in"));

Constructor Summary

BufferedReader(Reader in).
Creates a buffering character-input stream that uses a default-sized input buffer.

BufferedReader(Reader in, int size)
Creates a buffering character-input stream that uses an input buffer of the specified size.

BufferedReader Methods Detail

1). public int read() throws IOException

Reads a single character.

The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached.

2). public String readLine() throws IOException

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed. It returns A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.

3). public long skip(long n) throws IOException

it Skips the given character while reading the line. It returns the number of characters skipped.

4). public boolean ready() throws IOException

It tells Tells whether this stream is ready to be read. A buffered character stream is ready if the buffer is not empty, or if the underlying character stream is ready. It returns True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.

5). public boolean markSupported()

It Tells whether this stream supports the mark() operation, which it does. It returns true if and only if this stream supports the mark operation.

6). public void mark(int readAheadLimit) throws IOException

It Marks the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point.

7). public void reset() throws IOException

It Resets the given stream to the most recent mark.

8). public void close() throws IOException

It Closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.

9). public Stream lines()

Returns a Stream, the elements of which are lines read from this BufferedReader. The Stream is lazily populated, i.e., read-only occurs during the terminal stream operation. The reader must not be operated on during the execution of the terminal stream operation. Otherwise, the result of the terminal stream operation is undefined.

After execution of the terminal stream operation, there are no guarantees that the reader will be at a specific position from which to read the next character or line.

If an IOException is thrown when accessing the underlying BufferedReader, it is wrapped in an UncheckedIOException which will be thrown from the Stream method that caused the read to take place. This method will return a Stream if invoked on a BufferedReader that is closed. Any operation on that stream that requires reading from the BufferedReader after it is closed, will cause an UncheckedIOException to be thrown.

1) Use BufferedReader without try-with-resources

import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class BufferedReaderExample1 { public static void main(String[] args) { BufferedReader br = null; try { String line; br = new BufferedReader(new FileReader("/home/devuser/hello.txt"));//C:\\devuser\\hello.txt" for Window while ((line = br.readLine()) != null) { System.out.println(line); } } catch(IOException e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); } catch(IOException ex) { ex.printStackTrace(); } } } } output:

2) Use BufferedReader with try-with-resources

import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class BufferedReaderExample2 { public static void main(String[] args) { BufferedReader br = null; try (BufferedReader bufferedReader = new BufferedReader(new FileReader("/home/devuser/hello.txt")))//C:\\devuser\\hello.txt" for Windows { String currLine; while ((currLine = bufferedReader.readLine()) != null) { System.out.println(currLine); } } catch (IOException e) { e.printStackTrace(); } } } output:

3). Buffered reader method example

import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; class BufferedReaderExample3 { public static void main(String[] args) throws IOException { FileReader fr = new FileReader("file.txt"); BufferedReader br = new BufferedReader(fr); char c[] = new char[20]; //illustrating markSupported() method if (br.markSupported()) { System.out.println("mark() method is supported"); br.mark(100); } br.skip(5); if (br.ready()) { System.out.println(br.readLine()); br.read(c); for (int i = 0; i < 20; i++) { System.out.print(c[i]); } System.out.println(); br.reset(); for (int i = 0; i < 8; i++) { System.out.print((char) br.read()); } } } } output:

Post/Questions related to Java BufferedReader Tutorial

How to download a pdf file using java?
How to copy a file from one directory to another in java?
How to put a file into a folder in java?
How to create a directory in java?
How to read all files in a directory in java?
How to delete all files in a directory in java?
How to take input using the buffered reader in java?
How to read the next line using the buffered reader in java?
How to read multiple lines using the buffered reader in java?
What is the use of buffered reader in java?
Do we need to close the Bufferedreader in Java?
How to check if directory exists in java?
How to Download File from URL in Java
Read file to byte[] array in Java Java File Reader Examples
Java file writer Example
java.nio.file.Files methods with Examples
Java File Path, Absolute Path, and Relative Path
Path toString() method in Java Create New File in Java

In this article, we have seen Java BufferedReader Tutorial with Examples. All source code in the article can be found in the GitHub repository.