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.