BufferedOutputStream in Java | Methods, Example

A BufferedOutputStream in Java is a concrete subclass of FilterOutputStream class.

It wraps (buffers) an output stream into a buffered stream and makes write operations on the stream more efficient and fast.

Java BufferedOutputStream adds a buffer to an output stream that stores individual data (in bytes) temporarily into a memory buffer.

It is used to speed up the output by reducing the number of disk or file writes by adding an additional layer of functionality around the underlying stream.

For example, suppose we write data into a file using FileOutputStream as: fos.write(ch);

Here, we invoke write() method to write a character (ch) into a file. Then, the operating system writes character ch into the file.

Now suppose we need to write 50 characters into a file. The underlying operating system would be called 50 times to write the 50 characters. It will waste a lot of time.

But if we buffer output to a file by wrapping a FileOutputStream within a BufferedOutputStream, it will first write characters into a temporary block of memory called a buffer.

And then all the characters from the buffer will be at once written into a file by the operating system. Thus, the buffered output stream will take less time to make write operations fast.

Working of BufferedInputStream in Java


In Java BufferedOutputStream, a buffer is internally between the program and disk (destination). During the write operation, the individual data are first stored temporarily in the memory buffer.

When the memory buffer is full, all data from the buffer are written to the disk once, as shown in the below fig.

Working of BufferedOutputStream in Java

A buffered output stream adds more efficiency than writing data directly into a stream and makes the performance fast. This is because the number of times accessing the disk is reduced.

Java BufferedOutputStream class declaration


BufferedOutputStream class is derived from class FilterOutputStream, which has OutputStream as a base class. It implements Closeable, Flushable, and AutoCloseable interfaces.

The general declaration of BufferedInputStream class is as follows:

public class BufferedOutputStream
   extends FilterOutputStream
     Closeable, Flushable, AutoCloseable

BufferedOutputStream was added in Java 1.0 version. It is present in java.io.BufferedOutputStream package.

Constructor of BufferedOutputStream class


To wrap OutputStream, BufferedInputStream class provides two constructors that are as follows:

1. BufferedOutputStream(OutputStream outputStream): This constructor creates the new buffered output stream object that buffers an output stream specified by outputStream.

It uses the default buffer size for writing the data to the specified output stream. The general syntax to wrap FileOutputStream into BufferedOutputStream is as follows:

// Creates an instance of FileOutputStream with specified file path.
     FileOutputStream fos = new FileOutputStream(String path);

// Creates a BufferedOutputStream object and passes reference variable fos to its constructor.
     BufferedInputStream buffer = new BufferInputStream(fos); // Wrapping the file stream in a BufferedInputStream.

2. BufferedOutputStream(OutputStream outputStream, int size): This constructor creates a new buffered output stream object that buffers an output stream with specified buffer size.

The general syntax to wrap FileOutputStream into the buffered stream is as follows:

// Creates a BufferedOutputStream object with specified size of internal buffer.
     BufferedOutputStream buffer = new BufferInputStream(fos, int size);

For example:
     BufferedOutputStream buffer = new BufferedOutputStream(fos, 1024); // Here, buffer size is defined as 1024 bytes.

BufferedOutputStream Methods in Java


BufferedOutputStream class in Java does not define any new methods. All the methods in BufferedOutputStream are inherited from the OutputStream class.

Some important methods are as follows:

1. void write(int b): This method writes the specified byte to the buffered output stream.

2. void write(byte[ ] b, int n, int m): This method writes the bytes from the specified byte-input stream m into a specified byte array, starting from the given nth byte.

3. void flush(): This method flushes the buffered output stream. It can be used to clear the internal buffer by forcing the output stream to write all data present in the buffer to the destination file.

4. void close(): This method closes the buffered output stream. Once the method is invoked, we cannot write the data again.

All of the above methods will throw an exception named IOException if any error occurs.

Example Program based on Java BufferedOutputStream


1. Let’s take an example program where we will improve the efficiency of writing data into a file using buffered output stream. In this program, we will write the textual information in the file using BufferedOutputStream object.

Program source code 1:

package javaProgram;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
public class BufferedOutputStreamEx {
public static void main(String[] args)
{
 try
 {
// Create a FileOutputStream object to connect myfile to FileOutputStream.	 
     FileOutputStream fos = new FileOutputStream("D:\\myfileout.txt");
  
// Create a BufferedOutputStream object to wrap FileOutputStream in BufferedOutputStream.
     BufferedOutputStream bos = new BufferedOutputStream(fos);
 
     String s="Welcome to Scientech Easy";    
     byte b[]=s.getBytes(); // Converting String into array bytes.
     bos.write(b); // Write data to the output stream.
     bos.close(); // Closing output stream.

 System.out.println("Successfully written...");   
}catch(Exception e) {
   System.out.println(e);
 }
 }}
Output:
            Successfully written...

When we run the above program, the myfileout.txt file contains the text: “Welcome to Scientech Easy”.

2. Let’s create a program to flush all data present in the internal buffer to the file. Look at the source code below.

Program source code 2:

package javaProgram;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedOutputStreamEx {
public static void main(String[] args) throws IOException
{
   String data = "Hello Java";	
// Create a FileOutputStream object to connect myfile to FileOutputStream.	 
     FileOutputStream fos = new FileOutputStream("D:\\myfileout.txt");
  
// Create a BufferedOutputStream object to wrap FileOutputStream in BufferedOutputStream.
     BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);
   
   bos.write(data.getBytes()); // Writing data to output stream.
   bos.flush(); // Flushing all data from internal buffer to destination file.
   bos.close();
 System.out.println("Successfully written...");  
 }}
Output:
            Successfully written...

When we run the above program, the file myfile.txt will contain the following text: “Hello Java”.


Hope that this tutorial has covered almost all the important points related to BufferedOutputStream in Java with example programs. I hope that you will have understood the basic concepts of buffered output stream class.
Thanks for reading!!!
Next ⇒ FilterInputStream in Java

⇐ Prev Next ⇒