You can do it with using a FileOutputStream and the writeTo method.
ByteArrayOutputStream byteArrayOutputStream = getByteStreamMethod();
try(OutputStream outputStream = new FileOutputStream("thefilename")) {
byteArrayOutputStream.writeTo(outputStream);
}
Source: "Creating a file from ByteArrayOutputStream in Java." on Code Inventions
Answer from Suresh Atta on Stack OverflowVideos
You can do it with using a FileOutputStream and the writeTo method.
ByteArrayOutputStream byteArrayOutputStream = getByteStreamMethod();
try(OutputStream outputStream = new FileOutputStream("thefilename")) {
byteArrayOutputStream.writeTo(outputStream);
}
Source: "Creating a file from ByteArrayOutputStream in Java." on Code Inventions
You can use a FileOutputStream for this.
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("myFile"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Put data in your baos
baos.writeTo(fos);
} catch(IOException ioe) {
// Handle exception here
ioe.printStackTrace();
} finally {
fos.close();
}
ByteArrayOutputStream writes bytes to a byte array in memory. Not to any other destination, such as a file or a network socket. After writing the data, you can get the byte array by calling toByteArray() on it.
BufferedOutputStream wraps another, underlying OutputStream and provides buffering for that underlying stream, to make I/O operations more efficient. The underlying stream can be any kind of OutputStream, for example one that writes to a file or a network socket.
Why you might want to use buffering: Writing a large block of data to the file system is more efficient than writing byte by byte. If your program needs to write many small pieces of data, it's more efficient to first gather these small pieces in a buffer and then write the entire buffer to disk at once. This is what BufferedOutputStream does automatically for you.
Just look at the javadoc:
ByteArrayOutputStream:
This class implements an output stream in which the data is written into a byte array.
BufferedOutputStream:
The class implements a buffered output stream. By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written.
So, those are really two very different things:
- the first one you use when you know that you have some data that in the end you need as array of bytes
- the second one is just a wrapper around any other kind of output stream - which adds buffering.
That is all there is to this!
And if you want to experience a different behavior: create a buffered one that writes to a file, and an array one. Then just keep pushing bytes into each one. The array one will cause a memory problem at some point, the other one might not stop until all of your disk space is used up.
Heh, sounds like they copied and pasted code from different sources? :-P No, seriously, unless you need to inspect the decompressed data, you can just use a BufferedOutputStream for both compression and decompression.
The ByteArrayOutputStream is more memory hogging since it stores the entire content in Java's memory (in flavor of a byte[]). The FileOutputStream writes to disk directly and is hence less memory hogging. I don't see any sensible reason to use ByteArrayOutputStream in this particular case. It is not modifying the individual bytes afterwards. It just get written unchanged to file afterwards. It's thus an unnecessary intermediate step.