Buffered Input and Buffered Output Stream

Filter Course


Buffered Input and Buffered Output Stream

Published by: Dikshya

Published date: 25 Jun 2023

Buffered Input and Buffered Output Stream

Buffered Input Stream and Buffered Output Stream are classes that provide buffering capabilities to improve the performance of input and output operations. They internally maintain a buffer, which is a region of memory used to temporarily hold data, reducing the number of actual read or write operations performed.

Buffered Input Stream:

A Buffered Input Stream is a wrapper class that adds buffering functionality to an underlying input stream. It improves input performance by reducing the number of system calls made to read data from the underlying source. When you read data from a Buffered Input Stream, it fetches a larger chunk of data from the underlying input stream into its internal buffer, and subsequent read operations are served from the buffer until it is exhausted.

Buffered Output Stream:

A Buffered Output Stream is a wrapper class that adds buffering functionality to an underlying output stream. It improves output performance by reducing the number of system calls made to write data to the underlying destination. When you write data to a Buffered Output Stream, it accumulates the data in its internal buffer and flushes the buffer to the underlying output stream either when it is full or explicitly requested by the program.

Here's an example in Java that demonstrates the usage of Buffered Input Stream and Buffered Output Stream:

import java.io.*;

public class BufferedIOExample {
    public static void main(String[] args) {
        try {
            // File Input Stream with Buffered Input Stream
            FileInputStream fileInputStream = new FileInputStream("input.txt");
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

            // Read data using Buffered Input Stream
            int data;
            while ((data = bufferedInputStream.read()) != -1) {
                // Process the data
                System.out.print((char) data);
            }

            bufferedInputStream.close();

            // File Output Stream with Buffered Output Stream
            FileOutputStream fileOutputStream = new FileOutputStream("output.txt");
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

            // Write data using Buffered Output Stream
            String text = "Hello, World!";
            byte[] bytes = text.getBytes();

            bufferedOutputStream.write(bytes);
            bufferedOutputStream.flush();  // Flush the buffer to ensure all data is written

            bufferedOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In the above example, a Buffered Input Stream is used to read data from the "input.txt" file. The Buffered Input Stream internally buffers the data read from the file, improving the overall read performance. Similarly, a Buffered Output Stream is used to write the string "Hello, World!" to the "output.txt" file. The Buffered Output Stream accumulates the data in its internal buffer and flushes it to the file when the buffer is full or explicitly flushed.

By using Buffered Input Stream and Buffered Output Stream, you can achieve better I/O performance when working with files or other input/output sources.