VOOZH about

URL: https://www.geeksforgeeks.org/java/java-io-bufferedoutputstream-class-java/

⇱ Java BufferedOutputStream Class - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java BufferedOutputStream Class

Last Updated : 3 Oct, 2025

In Java, the BufferedOutputStream class is used to write data to an output stream more efficiently. It adds a buffer to another output stream, reducing the number of I/O operations by temporarily storing data in memory before writing it to the destination (like a file).

It belongs to the java.io package and extends the FilterOutputStream class.

Class Declaration

public class BufferedOutputStream extends FilterOutputStream

  • Improves I/O performance by using a buffer to reduce direct disk access.
  • Data is written to the buffer, and only when the buffer is full (or flushed), it is sent to the underlying output stream.
  • Typically used with FileOutputStream.
  • Helps in writing byte data, such as images, text files, and binary data.

Constructors

  • BufferedOutputStream(OutputStream out): Creates a buffered output stream with a default buffer size of 8192 bytes.
  • BufferedOutputStream(OutputStream out, int size): Creates a buffered output stream with the specified buffer size.

Important Methods

MethodDescription
void write(int b)Writes a single byte to the output stream.
void write(byte[] b, int off, int len)Writes a portion of the byte array to the output stream.
void flush()Flushes the buffer and forces any buffered output bytes to be written.
void close()Closes the stream, flushing it first.

Example 1: Writing Data to File using BufferedOutputStream

Output (content of output.txt):

BufferedOutputStream in Java Example

Example 2: Using flush() Method

Output (content of flush.txt):

Hello

Example 3: Writing Large Data Efficiently

Output (content of large.txt, first few lines):

Line 0

Line 1

Line 2

...

Advantages

  • Faster writing performance compared to unbuffered streams.
  • Reduces the number of physical write operations.
  • Allows writing large data efficiently.
Comment
Article Tags: