BufferedReader is a class which simplifies reading text from a character input stream. It buffers the characters in order to enable efficient reading of text data. BufferedReader comes in handy if we want to read text from any kind of input source whether that be files, sockets, or something else. Simply put, it enables us to minimize the number of I/O operations by reading chunks of characters and storing them in an internal buffer. While the buffer has data, the reader will read from it instead of directly from the underlying stream.
Like most of the Java I/O classes, BufferedReader implements Decorator pattern, meaning it expects a Reader in its constructor. In this way, it enables us to flexibly extend an instance of a Reader implementation with buffering functionality. BufferedReader can be configured to take any kind of input stream as an underlying source.
// Reading from file BufferedReader reader = new BufferedReader(new FileReader("input.txt")); // Reading from System.in which typically corresponds to the input from the keyboard BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Similarly, we could pass an input stream for reading from a socket, file or any imaginable type of textual input. The only prerequisite is that there is a suitable InputStream implementation for it.
BufferedReader vs Scanner
As an alternative, we could use the Scanner class to achieve the same functionality as with BufferedReader. However, there are significant differences between these two classes which can make them either more or less convenient for us, depending on our use case:
- BufferedReader is synchronized (thread-safe) while Scanner is not
- Scanner can parse primitive types and strings using regular expressions
- BufferedReader allows for changing the size of the buffer while Scanner has a fixed buffer size
- BufferedReader has a larger default buffer size
- Scanner hides IOException, while BufferedReader forces us to handle it
- BufferedReader is usually faster than Scanner because it only reads the data without parsing it
//Java program demonstrating BufferedReader class BufferedReaderDemo { public static void main(String[] args) throws IOException { FileReader fr = new FileReader("file.txt"); BufferedReader br = new BufferedReader(fr); char c[] = new char[20]; // Check if stream supports the mark() operation if(br.markSupported()) { // Marks the present position in the stream.Subsequent calls to reset() will attempt to reposition the stream to this point. br.mark(100); } // Skipping 8 characters br.skip(8); // Tells whether this stream is ready to be read. if(br.ready()) { // 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. System.out.println(br.readLine()); // Reads characters into an array, read(char c[],int off,int len) br.read(c); // Resets the stream to the most recent mark. br.reset(); } } }