BufferedReader and BufferedWriter in Java

In Java, BufferedReader and BufferedWriter are classes designed to handle character-based input and output operations efficiently. These classes are used for reading and writing text data (characters) from/to files or other input/output sources like network streams.

They work similarly to BufferedInputStream and BufferedOutputStream, but while the latter are used for byte-based data, BufferedReader and BufferedWriter are used for character-based data, making them ideal for handling text files.


What is BufferedReader?

BufferedReader is a class in Java that wraps around other Reader classes (like FileReader) to provide efficient reading of characters, arrays, and lines of text. It reads large chunks of data into a buffer, and then the program reads data from the buffer rather than directly from the underlying source, improving performance.

How Does BufferedReader Work?

  • Without buffering: Reading data character-by-character from a file (or other sources) can be slow, especially for large files, as it involves frequent I/O operations.
  • With buffering: BufferedReader reads large chunks of data from the file into an internal buffer. Subsequent reads are taken from the buffer, reducing the number of I/O operations and improving performance.

Constructors of BufferedReader

BufferedReader(Reader in)
BufferedReader(Reader in, int size)  // in: reader object (like FileReader), size: buffer size
  • Reader in: The underlying reader, such as FileReader, from which data will be read.
  • size: An optional parameter to specify the size of the buffer (default is typically 8 KB).

Key Methods of BufferedReader

  • read(): Reads a single character.
  • read(char[] cbuf, int off, int len): Reads characters into a portion of an array.
  • readLine(): Reads a line of text.
  • close(): Closes the reader and releases system resources.
Code Example

Explanation:

  • BufferedReader wraps the FileReader to add buffering.
  • br.readLine(): Reads one line of text at a time from the file example.txt, making it more efficient for text data.
  • Output: Each line of the file is printed to the console.

What is BufferedWriter?

BufferedWriter is a class in Java that wraps around other Writer classes (like FileWriter) to provide efficient writing of characters, arrays, and strings. It stores characters in a buffer before writing them to the file or output stream, reducing the number of I/O operations by writing large chunks of data at once.

How Does BufferedWriter Work?

  • Without buffering: Writing data character-by-character to a file can be inefficient, as each character is written directly to the file system, resulting in frequent I/O operations.
  • With buffering: BufferedWriter accumulates characters in a buffer and only writes the data when the buffer is full or when the flush() method is called, reducing the number of writes to the underlying system.

Constructors of BufferedWriter

BufferedWriter(Writer out)
BufferedWriter(Writer out, int size)  // out: writer object (like FileWriter), size: buffer size
  • Writer out: The underlying writer, such as FileWriter, to which data will be written.
  • size: An optional parameter to specify the size of the buffer (default is typically 8 KB).

Key Methods of BufferedWriter

  • write(String s): Writes a string to the output.
  • write(char[] cbuf, int off, int len): Writes characters from a portion of an array.
  • newLine(): Writes a newline character to the output.
  • flush(): Flushes the output stream, writing any buffered data to the file.
  • close(): Closes the writer and releases system resources.
Code Example

Explanation:

  • BufferedWriter wraps the FileWriter to add buffering.
  • bw.write(): Writes the string "Hello, BufferedWriter!" to the buffer.
  • bw.newLine(): Writes a new line to the file.
  • bw.flush(): Ensures that all buffered data is written to the file before closing the stream.
  • Output: The file output.txt contains the text written by the program.

Why Use BufferedReader and BufferedWriter?

  • Improved Performance:
    • Reading and writing large files one character at a time is inefficient because each read/write operation interacts with the file system.
    • BufferedReader and BufferedWriter improve performance by reading and writing large chunks of data at once.
  • Text File Processing:
    • BufferedReader is ideal for reading text files line-by-line (like log files, configuration files, etc.).
    • BufferedWriter is great for writing text data efficiently to files, especially when frequent writes are needed.

BufferedReader and BufferedWriter Example: Copying a Text File

Here’s an example of how to use BufferedReader and BufferedWriter together to copy a text file efficiently.

Code Example

Explanation:

  • BufferedReader reads each line from sourceFile.txt using readLine().
  • BufferedWriter writes each line to destinationFile.txt using write(), followed by newLine() to preserve the file’s structure.