BufferedOutputStream Class in Java

In this article, we will understand how to use BufferedOutputStream class in Java with an example. 

1. What is BufferedOutputStream? 

The BufferedOutputStream class, found in the java.io package, is a filtered output stream that uses an internal buffer to collect data before actually writing it to the underlying output stream. By accumulating data in a buffer and then writing it in chunks, it helps reduce the I/O cost. 

2. Under the Hood 

When you wrap an existing output stream (like FileOutputStream) with a BufferedOutputStream, you can write data to the buffer. Once the buffer is full, the data is then flushed to the original output stream in one go. 

3. A Simple Use Case 

Here's a basic example to demonstrate writing to a file using BufferedOutputStream:

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufferedOutputStreamExample {

    public static void main(String[] args) {
        String content = "Welcome to the world of BufferedOutputStream in Java!";
        
        try (FileOutputStream fos = new FileOutputStream("output.txt");
             BufferedOutputStream bos = new BufferedOutputStream(fos)) {
            
            bos.write(content.getBytes());
            System.out.println("Data successfully written to output.txt!");

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

In this code, the given string content is written to the file output.txt via BufferedOutputStream

4. Key Points

Boost in Performance: The major advantage of BufferedOutputStream is its ability to improve I/O performance by minimizing the number of write operations to the underlying stream. 

Buffer Size Customization: The default buffer size for BufferedOutputStream is 8KB. However, if required, you can define a custom size during instantiation using the new BufferedOutputStream(fos, bufferSize)

Always Flush: It's crucial to flush or close the BufferedOutputStream once you're done writing, as this ensures that any remaining data in the buffer is written to the underlying stream. 

5. When to Use BufferedOutputStream?

Frequent Writes: Optimizes performance when performing many small, frequent write operations.

Slow Outputs: Enhances efficiency when writing to slow destinations like network sockets or hard drives. 

Consistent Performance: Provides a uniform write performance across varying I/O loads. 

Disk I/O: Reduces wear and enhances performance on storage devices by enabling larger, sequential writes. 

Resource Conservation: Decreases system resource usage by minimizing the number of write operations. 

Stream Chaining: Useful when working with multiple streams in a chain, especially if some are already buffered. 

Note: Always flush() after writing to ensure all buffered data is processed.

6. Conclusion

Using BufferedOutputStream is an excellent way to optimize I/O operations in Java, especially for applications that require extensive and frequent writes. By buffering data and reducing the direct interactions with the data source, this class helps in achieving better application throughput and responsiveness.

Related Java I/O Classes

Comments