BufferedOutputStream

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java BufferedOutputStream Class

Java BufferedOutputStream class is used for buffering an output stream. It internally uses buffer to store data. It adds more efficiency than to write data directly into a stream. So, it makes the performance fast.

For adding the buffer in an Output Stream, use the BufferedOutputStream class. Let's see the syntax for adding the buffer in an Output Stream:

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

Java Buffered Output Stream class declaration

Let's see the declaration for Java.io. Buffered Output Stream class:

  1. public class BufferedOutputStream extends FilterOutputStream  

Java Buffered Output Stream class constructors

Constructor

Description

Buffered Output Stream(Output Stream os)

It creates the new buffered output stream which is used for writing the data to the specified output stream.

Buffered Output Stream(Output Stream 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 Buffered Output Stream 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 byte array, starting with the given offset

void flush()

It flushes the buffered output stream.

Example of Buffered Output Stream class:

In this example, we are writing the textual information in the Buffered Output Stream object which is connected to the File Output Stream object. The flush () flushes the data of one stream and send it into another. It is required if you have connected the one stream with another.

  1. package com.javatpoint;  
  2. import java.io.*;  
  3. public class BufferedOutputStreamExample{    
  4. public static void main(String args[])throws Exception{    
  5.      FileOutputStream fout=new FileOutputStream("D:\testout.txt");    
  6.      BufferedOutputStream bout=new BufferedOutputStream(fout);    
  7.      String s="Welcome to javaTpoint.";    
  8.      byte b[]=s.getBytes();    
  9.      bout.write(b);    
  10.      bout.flush();    
  11.      bout.close();    
  12.      fout.close();    
  13.      System.out.println("success");    
  14. }    
  15. }  

Output:

Success

testout.tx