Java Bufferedoutputstream Class

The Java BufferedOutputStream class is employed to buffer an output stream, utilizing an internal buffer to store data. This approach enhances efficiency compared to writing data directly to a stream, resulting in improved performance.

To introduce buffering in an OutputStream, utilize the BufferedOutputStream class. Let's explore the syntax for incorporating buffering in an OutputStream:

Example

OutputStream os= new BufferedOutputStream(new FileOutputStream("D:\\IO Package\\testout.txt"));

Java BufferedOutputStream class declaration

Now, let's take a look at the declaration of the Java.io.BufferedOutputStream class:

Example

public class BufferedOutputStream extends FilterOutputStream

Java BufferedOutputStream class constructors

Constructor Description
BufferedOutputStream(OutputStream os) It creates the new buffered output stream which is used for writing the data to the specified output stream.
BufferedOutputStream(OutputStream os, int size) It creates the new buffered output stream which is used for writing the data to the specified output stream with a specified buffer size.

Java BufferedOutputStream class methods

Method Description
void write(int b) It writes the specified byte to the buffered output stream.
void write(byte[] b, int off, int len) It write the bytes from the specified byte-input stream into a specified bytearray, starting with the given offset
void flush() It flushes the buffered output stream.

Example of BufferedOutputStream class:

In this instance, we are inputting text data into the BufferedOutputStream instance linked to the FileOutputStream instance. When flush is called, it transmits the content from one stream to another. This step is necessary when two streams are connected.

Example

package com.example;

import java.io.*;

public class BufferedOutputStreamExample{  

public static void main(String args[])throws Exception{  

	 FileOutputStream fout=new FileOutputStream("D:\\testout.txt");  

	 BufferedOutputStream bout=new BufferedOutputStream(fout);  

	 String s="Welcome to Java.";  

	 byte b[]=s.getBytes();  

	 bout.write(b);  

	 bout.flush();  

	 bout.close();  

	 fout.close();  

	 System.out.println("success");  

}  

}

Output:

Output

Success

testout.txt

Example

Welcome to Java.

Input Required

This code uses input(). Please provide values below: