Java - BufferedOutputStream Class



Introduction

The Java BufferedOutputStream class implements a buffered output stream. By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written.

Class declaration

Following is the declaration for Java.io.BufferedOutputStream class βˆ’

public class BufferedOutputStream
   extends FilterOutputStream

Field

Following are the fields for Java.io.BufferedOutputStream class βˆ’

  • protected byte[] buf βˆ’ This is the internal buffer where data is stored.

  • protected int count βˆ’ This is the number of valid bytes in the buffer.

  • protected OutputStream out βˆ’ This is the underlying output stream to be filtered.

Class constructors

Sr.No. Constructor & Description
1

BufferedOutputStream(OutputStream out)

This creates a new buffered output stream to write data to the specified underlying output stream.

2

BufferedOutputStream(OutputStream out, int size)

This creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.

Class methods

Sr.No. Method & Description
1 void flush()

This method flushes this buffered output stream.

2 void write(byte[] b, int off, int len)

This method writes len bytes from the specified byte array starting at offset off to this buffered output stream.

3 void write(int b)

This method writes the specified byte to this buffered output stream.

Methods inherited

This class inherits methods from the following classes βˆ’

  • Java.io.FilterOutputStream
  • Java.io.Object

Here are the examples of how to use the BufferedOutputStream class in Java, each demonstrating a different aspect of its functionalityβˆ’

Example - Writing Text to a File Using BufferedOutputStream

This example demonstrates how to write a string to a file using BufferedOutputStream.

package com.tutorialspoint;

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

public class BufferedOutputStreamDemo {
   public static void main (String[] args) {
      String text = "Hello, BufferedOutputStream!";
      // Create BufferedOutputStream and FileOutputStream to write content to a file	  
      try (FileOutputStream fos = new FileOutputStream("example.txt");
         BufferedOutputStream bos = new BufferedOutputStream(fos)) { 
         // write contents to the BufferedOutputStream     
         bos.write(text.getBytes());
		 // Ensures all data is written to the file
         bos.flush(); 
         System.out.println("Data written successfully!");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Data written successfully!

After running the program, you can check the content of example.txt in the current directory.

Hello, BufferedOutputStream!

Explanation

  • This program reads a file example.txt using a BufferedInputStream wrapped around a FileInputStream.

  • It reads one byte at a time and prints it as a character.

  • The buffer improves efficiency by reducing the number of I/O operations.

Example - Writing Bytes in Chunks Using a Buffer

This example shows how to write an array of bytes in chunks to a file.

package com.tutorialspoint;

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

public class BufferedOutputStreamDemo {
   public static void main(String[] args) {
      byte[] data = "This is an example of chunked writing.".getBytes();
      // Create BufferedOutputStream and FileOutputStream to write content to a file
      try (FileOutputStream fos = new FileOutputStream("example.txt");
         BufferedOutputStream bos = new BufferedOutputStream(fos)) {
         // define a chunk size
         int chunkSize = 10;
         for (int i = 0; i < data.length; i += chunkSize) {
            // write content as per the chunk size
            int length = Math.min(chunkSize, data.length - i);
            bos.write(data, i, length);
         }
         // Ensures all data is written to the file
         bos.flush();
         System.out.println("Chunked data written successfully!");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Chunked data written successfully!

After running the program, you can check the content of example.txt in the current directory.

This is an example of chunked writing.

Explanation

  • The data is split into chunks of 10 bytes and written iteratively.

  • The write method with offset and length parameters ensures that only a subset of the array is written during each iteration.

Example - Copying a File Using BufferedInputStream and BufferedOutputStream

We've used a "example.txt" file with following contentβˆ’

Welcome to tutorialspoint.com

This example demonstrates copying the contents of one file to another.

package com.tutorialspoint;

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

public class BufferedOutputStreamDemo {
   public static void main(String[] args) {
      // Create BufferedInputStream and FileInputStream to read content from a file
      // Create BufferedOutputStream and FileOutputStream to write content to a file
      try (FileInputStream fis = new FileInputStream("example.txt");
         BufferedInputStream bis = new BufferedInputStream(fis);
         FileOutputStream fos = new FileOutputStream("destination.txt");
         BufferedOutputStream bos = new BufferedOutputStream(fos)) {
         // Buffer of 1 KB
         byte[] buffer = new byte[1024]; 
         int bytesRead;
         // read till last byte
         while ((bytesRead = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, bytesRead);
         }
         // Ensures all data is written to the file		 
         bos.flush();
         System.out.println("File copied successfully!");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

File copied successfully!

After running the program, you can check the content of example.txt in the current directory.

Welcome to tutorialspoint.com

Explanation

  • BufferedInputStream reads data from source.txt in chunks of 1024 bytes.

  • BufferedOutputStream writes the data to destination.txt.

  • The loop ensures all data from the source file is read and written.

Advertisements