Java BufferedOutputStream Class with Examples

Java is used in various methods and functions. We are esteemed to learn the BufferedOutputStream in Java. Various examples and explanations are explained.

BufferedOutput is used to write an array of output data.

Explanation of bufferedouputstream in Java

  • To buffer an output stream, use the Java BufferedOutputStream class.
  • It stores data internally in a buffer. Compared to writing data straight into a stream, it increases efficiency considerably, it accelerates the performance.
  • Use the BufferedOutputStream class to add a buffer to an OutputStream.

Syntax:

OutputStream os= new BufferedOutputStream(new FileOutputStream)

Java BufferedOutputStream class declaration:

public class BufferedOutputStream extends FilterOutputStream

Working:

  • An internal buffer of 8192 bytes is maintained by the BufferedOutputStream.
  • The internal buffer is where the bytes are written during the write process rather than the disk.
  • The entire buffer is written to the disk once it is full or the stream is closed.
  • As a result, there are less communications to the disk. This is the reason why BufferedOutputStream writes bytes more quickly.

To create BufferedOutputStream:

  • We need to import the java.io.BufferedOutputStream package before we can create a BufferedOutputStream. Here’s how to construct the output stream after importing the package.
FileOutputStream file = new FileOutputStream(String path);

BufferedOutputStream buffer = new BufferOutputStream(file);

Constructor Table:

              Constructor Description
BufferedOutputStream(OutputStream os) In order to write data to the designated output stream, it generates a new buffered output stream.
BufferedOutputStream(OutputStream os, int size) In order to write data to the specified output stream with the specified buffer size, a new buffered output stream is created.

Class Methods:

Method  Description 
void write(int b) It adds the designated byte to the output stream that has been buffered.
void write(byte[] b, int off, int len) Beginning at the provided offset, it writes the bytes from the supplied byte-input stream into a specified byte array.
void flush() The buffered output stream is flushed.

Write() method:

  • Write() – Transfers one byte to the output stream’s internal buffer
  • Writes the bytes from the array to the output stream with the syntax write(byte[] array).
  • Write(byte[] arr, int start, int length) – initializes an array at position start and writes the number of bytes equal to length to the output stream.

Flush():

  • Using the flush() method, we may empty the internal buffer. via using this technique, all data in the buffer must be written to the destination file via the output stream.

close():

  • The close() method can be used to end the buffered output stream. The output stream cannot be used to write data once the method has been called.
Methods Description
protected byte[] Buf – The data are stored in the internal buffer.
protected int

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 TechVidvan";    
     byte b[]=s.getBytes();    
     bout.write(b);    
     bout.flush();    
     bout.close();    
     fout.close();    
     System.out.println("Learning Java");    
}    
}

Output:
Learning Java

testout.txt
Welcome to TechVidvan

Example:

public class Main {
    public static void main(String[] args) {

        String data = "TechVidvan Java Course";

        try {
            
            FileOutputStream file = new FileOutputStream(" flush.txt");

                        BufferedOutputStream buffer = new BufferedOutputStream(file);

                        buffer.write(data.getBytes());

            
            buffer.flush();
            System.out.println("Data are stored in TechVidvan");
            buffer.close();
        }

        catch(Exception e) {
            e.getStackTrace();
        }
    }
}

Output:
Data are stored in TechVidvan

Fields:

  • protected byte[] buf :The data storage internal buffer.
  • protected int count:The buffer’s valid byte count is shown by the protected int count.

Difference between buffered and unbuffered output in Java:

  • For sequential files, the default is BUFFERED.
  • The default setting for DIRECT files is UNBUFFERED.
  • Specifies that each record in a RECORD file must pass through intermediate storage buffers on its way to and from a data collection. This enables processing in both the move and locate modes.

Main purposes

  • To write the data (in bytes) more quickly, the java.io package’s BufferedOutputStream class is utilized in conjunction with other output streams.
  • The OutputStream abstract class is extended by it.

Buffer used in JavaStreams:

  • A section of memory called a buffer is used to hold a stream of data from peripheral devices.
  • This stream of data is then gathered and saved in variables from this buffer.
  • One way to describe a stream is as an ongoing data flow.

Difference between BufferedOutput Stream and Output Stream:

  • The distinction is that each time you pass an unbuffered byte to write, it makes a write call to the underlying system.
  • The system call to write the data is made only after the flush command has been called since the buffered output stream stores the data to be written in a buffer.

Benefits of BufferedOutput Stream:

  • It Stores data internally by using a buffer
  • It gives more efficiency to write data directly into the stream.
  • Performances are more fast and efficient.

Conclusion

We have extensively learn the Buffer OutputStream in Java. It give more essential of buffer and data stream usages.Examples are uses for essenting write data in output stream.