Why use BufferedReader and BufferedWriter Classses in Java

Isaac Jumba
3 min readSep 22, 2015

--

Both BufferedReader and BufferedWriter in java are classified as buffered I/O streams. Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.

For unbuffered I/O stream, each read or write request is handled directly by the underlying OS. This can make a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive. This means that instead of using BufferedReader, one can use the Scanner class, and instead of using BufferedWriter, one can use PrintWriter.

BufferedReader is a class in Java that reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, lines and arrays. The buffer size may be specified. If not, the default size, which is predefined, may be used.

In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore good practice to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,

FileReader reader = new FileReader(“MyFile.txt”);
BufferedReader bufferedReader = new BufferedReader(reader);

will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.

A BufferedWriter on the other hand is a java class that writes text to a character-output stream, while buffering characters so as to provide for the efficient writing of single characters, strings and arrays.

A newLine() method is provided, which uses the platform’s own notion of line separator as defined by the system property line.separator. Calling this method to terminate each output line is therefore preferred to writing a newline character directly.

In most occasions, a Writer sends its output immediately to the underlying character or byte stream. Unless prompt output is required, it is good practice to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters. For example,

File outputFile = new File("output.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));

will buffer the PrintWriter’s output to the file. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient.

Both BufferedReader and BufferedWriter are used in order to achieve greater efficiency through use of buffers. A data buffer is generally a region in memory that is temporarily used. Instructing a BufferedWriter to write on a file, it does not do it directly. Rather, it stores what you want it to write in a buffer and writes it onto the file when you tell it to perform a flush operation. Flushing is the operation that tells the BufferedWriter to write everything onto the output file. Using a buffer is what makes both BufferedReader and BufferedWriter fast and efficient.

It is recommended to use buffered I/O streams as opposed to Scanner and PrintWriter classes because of the following reasons:

  1. They have significantly larger buffer memory than Scanner and Print Writer. It is recommended to use BufferedReader if you want to get long strings from a stream, and use Scanner if you want to parse specific type of token from a stream.
  2. Buffered Streams are synchronous while unbuffered are not.This means you can work with multiple threads when using Buffered Streams.
  3. Scanner is memory and CPU heavy when compared to BufferedReader because it internally uses “regular expressions” for matching your “nextXXX” as opposed to just reading everything till the end of line as in the case of a regular Reader.
  4. BufferedReader is a bit faster as compared to Scanner.

--

--