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 Overflow
🌐
Apache Commons
commons.apache.org › proper › commons-io › javadocs › api-2.4 › src-html › org › apache › commons › io › output › ByteArrayOutputStream.html
ByteArrayOutputStream - Apache Commons
039 * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in · 040 * this class can be called after the stream has been closed without · 041 * generating an <tt>IOException</tt>. 042 * <p> 043 * This is an alternative implementation of the {@link java.io.ByteArrayOutputStream} 044 * class.
🌐
Bureau of Economic Geology
beg.utexas.edu › lmod › agi.servlet › doc › detail › java › io › ByteArrayOutputStream.html
: Class ByteArrayOutputStream
public ByteArrayOutputStream() Creates a new byte array output stream. The buffer capacity is initially 32 bytes, though its size increases if necessary. public ByteArrayOutputStream(int size) Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
🌐
Tutorialspoint
tutorialspoint.com › home › java/io › bytearrayoutputstream close method in java
Understanding ByteArrayOutputStream close Method in Java
February 13, 2026 - The Java ByteArrayOutputStream close() method is used to release resources associated with the stream. However, unlike streams that rely on external resources (e.g., files or network connections), calling close() on a ByteArrayOutputStream has no real effect because it operates entirely on memory.
🌐
Coderanch
coderanch.com › t › 693112 › java › Writing-byte-array-outputstream
Writing a byte-array into an outputstream (I/O and Streams forum at Coderanch)
have to use a ByteArrayOutputStream to get the byte-array into an outputstream. Not true. Many output classes have write methods that write bytes from an array. Look at the API doc.
🌐
Baeldung
baeldung.com › home › java › java io › convert an outputstream to a byte array in java
Convert an OutputStream to a Byte Array in Java | Baeldung
March 3, 2025 - public class DrainableOutputStream extends FilterOutputStream { private final ByteArrayOutputStream buffer; public DrainableOutputStream(OutputStream out) { super(out); this.buffer = new ByteArrayOutputStream(); } @Override public void write(byte b[]) throws IOException { buffer.write(b); super.write(b); } public byte[] toByteArray() { return buffer.toByteArray(); } }
Find elsewhere
🌐
Lsu
ld2016.scusa.lsu.edu › java › 7-docs › api › java › io › class-use › ByteArrayOutputStream.html
Uses of Class java.io.ByteArrayOutputStream (Java Platform SE 7 )
No usage of java.io.ByteArrayOutputStream · Overview · Package · Class · Use · Tree · Deprecated · Index · Help · Java™ Platform Standard Ed. 7 · Prev · Next · Frames · No Frames · All Classes · Submit a bug or feature For further API reference and developer documentation, ...
🌐
Apache Commons
commons.apache.org › proper › commons-io › javadocs › api-2.5 › org › apache › commons › io › output › ByteArrayOutputStream.html
ByteArrayOutputStream (Apache Commons IO 2.5 API)
This is an alternative implementation of the ByteArrayOutputStream class. The original implementation only allocates 32 bytes at the beginning. As this class is designed for heavy duty it starts at 1024 bytes. In contrast to the original it doesn't reallocate the whole memory block but allocates ...
🌐
Tutorialspoint
tutorialspoint.com › java › java_bytearrayoutputstream.htm
Java - ByteArrayOutputStream
import java.io.*; public class ByteStreamTest { public static void main(String args[])throws IOException { ByteArrayOutputStream bOutput = new ByteArrayOutputStream(12); while( bOutput.size()!= 10 ) { // Gets the inputs from the user bOutput.write("hello".getBytes()); } byte b [] = bOutput.toByteArray(); System.out.println("Print the content"); for(int x = 0; x < b.length; x++) { // printing the characters System.out.print((char)b[x] + " "); } System.out.println(" "); int c; ByteArrayInputStream bInput = new ByteArrayInputStream(b); System.out.println("Converting characters to Upper case " ); for(int y = 0 ; y < 1; y++ ) { while(( c = bInput.read())!= -1) { System.out.println(Character.toUpperCase((char)c)); } bInput.reset(); } } }
🌐
Jenkov
jenkov.com › tutorials › java-io › bytearrayoutputstream.html
Java ByteArrayOutputStream
This tutorial explains how to use the ByteArrayOutputStream in Java IO to write data to an OutputStream and capture that data in a byte array.
Top answer
1 of 3
13

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.

2 of 3
11

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.

🌐
Quora
quora.com › What-is-the-difference-between-the-ByteArrayOutputStream-and-BufferedOutputStream-in-Java
What is the difference between the ByteArrayOutputStream and BufferedOutputStream in Java? - Quora
Answer: ByteArrayOutputStream uses a byte array in memory as its storage medium. It is the end point of a stream. When the output operation is complete, you will take the resulting byte array as a singular object and use it for some purpose ...
🌐
Programiz
programiz.com › java-programming › bytearrayoutputstream
Java ByteArrayOutputStream (With Examples)
The ByteArrayOutputStream class of the java.io package can be used to write an array of output data (in bytes).