Java IO Tutorial - Java OutputStream

There are three important methods defined in the abstract superclass OutputStream: write(), flush(), and close().

The write() method writes bytes to an output stream. 
It has three versions allowsing us to write one byte or multiple bytes at a time. 

The flush() method is used to flush any buffered bytes to the data sink. 
The close() method closes the output stream.

To use the BufferedOutputStream decorator for better speed to write to a file, use the following statement:

BufferedOutputStream bos  = new BufferedOutputStream(new FileOutputStream("your output file  path"));

To write data to a ByteArrayOutputStream, use

ByteArrayOutputStream baos  = new ByteArrayOutputStream();
baos.write(buffer); // buffer is a  byte   array