The class ByteArrayOutputStream does just the opposite functionality of ByteArrayInputStream.

The ByteArrayInputStream reads data from the array and now the ByteArrayOutputStream writes the data to the byte array. All the characters of the array can be written or a few also. In the following program, both functionalities are illustrated.



In the above statement, complete byte array alphabets is written to the ByteArrayOutputStream object, bao1.

bao2.write( alphabets, 2, 4 );

To ByteArrayOutputStream object, bao2, a part of the array is written; four characters starting from second.

It is a good practice to close the streams when the job is over as with bao1.close().

Pass your suggestions or comments on this tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *