< Prev
Next >


ByteArrayOutputStream class is a subclass of OutputStream abstract class. It is used to write content of a byte array to the output stream by performing two steps in sequence -

Constructor :

This constructor creates a ByteArrayOutputStream object for writing out the contents of a byte array. Example-:
String str = "Learning about Input Streams";
byte[] b= str.getBytes();
ByteArrayOutputStream bais= new ByteArrayOutputStream();
We have initialized a byte array, b, to the value of a String str. This byte array is written to the internal buffer of ByteArrayOutputSteam. Data of internal buffer will be written out to an output stream.

Methods for writing bytes using ByteArrayOutputStream

Some methods that we generally use while working with ByteArrayOutputStream are shown in the table below :

Methods Description
This method flushes any buffered data to be written out of this output stream.
This method returns the current size of the buffer of ByteArrayOutputStream.

write(int b)
This method writes one byte at a time to this output stream.

This method writes a whole byte array at a time to this input stream.

writeTo(OutputStream out)
This method writes the content of ByteArrayOutputStream to the output stream passed to it in the parameters.

This method closes this output stream and also frees any resources connected with this output stream.

Writing one byte at a time using write(int b) method of ByteArrayInputStream

ByteArrayOutputStream writes the content of a byte array to its own internel buffer. Next, all the bytes in the internal buffer are written to a file using an output stream.
// A program to write a byte at a time using ByteArrayOutputStream. 

import java.io.*;

class A
public static void main(String... ar)
String str= new String("HelloJava");
byte b[]=str.getBytes();

FileOutputStream fos=new FileOutputStream("D:/TextBook.txt");
ByteArrayOutputStream baos= new ByteArrayOutputStream();

//Initial buffer size of ByteArrayOutputStream is 0
System.out.println("Initial Size of the buffer"+ baos.size()); 

//Writing one byte at a time to ByteArrayOutputStream's buffer
for(byte byt : b) 

System.out.println("Content written to ByteArrayOutputStream's buffer = "+baos.toString());
System.out.println("Size of the buffer after writing data to it = "+ baos.size()); 

//writing out the contents of BufferArrayOutputStream's buffer to a file.

//flusing all the bytes out of output stream

catch(IOException e)


Output is -:

Initial Size of the buffer = 0
Content written to ByteArrayOutputStream's buffer = HelloJava
Size of the buffer after writing data to it = 9

Program Analysis


Closing the output stream of ByteArrayOutputStream has no effect, we can still add more data to the buffer.

Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter

< Prev
Next >
< ByteArrayInputStream
BufferedInputStream >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
C++ and Python