When
we are dealing with data over the line, our option is bytes and one of
the handy Class in Java to deal with bytes and streams together is
ByteArrayOutputStream. When ever you want to write some bytes to
specific file while receiving the data you just need to use the method writeTo().
That all to create a file with specified name with incoming byte array stream.
Code to Create a file from ByteArrayOutputStream in Java:
OutputStream outStream = null;
ByteArrayOutputStream byteOutStream = null;
try {
outStream = new FileOutputStream("fileNameToSave");
byteOutStream = new ByteArrayOutputStream();
// writing bytes in to byte output stream
byteOutStream.write(bytes); //data
byteOutStream.writeTo(outStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
outStream.close();
}
That all to create a file with specified name with incoming byte array stream.