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
🌐
Programiz
programiz.com › java-programming › bytearrayoutputstream
Java ByteArrayOutputStream (With Examples)
In this tutorial, we will learn about Java ByteArrayOutputStream and its methods with the help of examples to write an array of output data.
🌐
Baeldung
baeldung.com › home › java › java io › guide to java outputstream
Guide to Java OutputStream | Baeldung
December 3, 2025 - Learn about the Java OutputStream's interfaces and common methods
🌐
Javatpoint
javatpoint.com › java-bytearrayoutputstream-class
Java ByteArrayOutputStream Class - javatpoint
Java Class Java is an output stream used for writing data to a file. If you have to write primitive values into a file, use class. You can write byte-oriented as well as character-oriented data through class.
🌐
Mkyong
mkyong.com › home › java › java – how to save byte[] to a file
Java - How to save byte[] to a file - Mkyong.com
September 17, 2020 - For JDK 1.7 and above, the NIO Files.write is the simplest solution to save byte[] to a file.
🌐
Jenkov
jenkov.com › tutorials › java-io › bytearrayoutputstream.html
Java ByteArrayOutputStream
November 15, 2019 - 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.
Find elsewhere
🌐
Reddit
reddit.com › r/javahelp › fileoutputstream v. bytearrayoutputstream: is there a noticeable difference in memory usage?
r/javahelp on Reddit: FileOutputStream v. ByteArrayOutputStream: is there a noticeable difference in memory usage?
December 20, 2022 -

Scenario: I have a REST API endpoint built with Spring Boot. The endpoint is used to dynamically generate an excel file based off input parameters. When the file is done being generated, it returns the file as an InputStreamResource. The main goal is minimum memory usage.

I'm using fastexcel to create the excel file, and I'm flushing it to the OutputStream after every row is written. Right now, I am using a FileOutputStream to write to disk. When the excel file is done being generated, I read it back in using InputStreamResource and stream the response. My thought process is that a ByteArrayOutputStream keeps everything in memory even if I'm flushing the excel file after every row, so I used the FileOutputStream. Does my logic track here? Or am I unnecessarily slowing things down with expensive filesystem IO?

🌐
Baeldung
baeldung.com › home › java › java io › writing byte[] to a file in java
Writing byte[] to a File in Java | Baeldung
December 11, 2025 - We’ll open an output stream to our destination file, and then we can simply pass our byte[] dataForWriting to the write method.
🌐
Tutorialspoint
tutorialspoint.com › java › java_bytearrayoutputstream.htm
Java - ByteArrayOutputStream
Home Whiteboard Practice Code Graphing Calculator Online Compilers Articles Tools ... Python TechnologiesDatabasesComputer ProgrammingWeb DevelopmentJava TechnologiesComputer ScienceMobile DevelopmentBig Data & AnalyticsMicrosoft TechnologiesDevOpsLatest TechnologiesMachine LearningDigital MarketingSoftware QualityManagement Tutorials View All Categories ... Java Vs. C++ ... The ByteArrayOutputStream class stream creates a buffer in memory and all the data sent to the stream is stored in the buffer.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › io › fileoutputstream
Write byte array to file with FileOutputStream - Java Code Geeks
October 26, 2013 - Write bytes from a specified byte array to this file output stream, using write(byte[] b) API method.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java tutorial › java bytearrayoutputstream
Java ByteArrayOutputStream | Examples of Java ByteArrayOutputStream
April 10, 2023 - In Java, ByteArrayOutputStream is a class that helps in writing common data into more than one file. Here, a byte array is used in order to write data that helps in writing data into multiple files.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Medium
medium.com › @lavishj77 › java-i-o-byte-stream-implementation-6acf5a9ec848
Java I/O Byte Stream Implementation | by Lavish Jain | Medium
April 16, 2022 - Used to read and write from and to a byte array. String text = "Awesome Java"; InputStream bis = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)); //read() method implementation is same as that of FileInputStream.ByteArrayOutputStream bos = new ByteArrayOutputStream(); //write() method implementation same as that of FileOutputputStream.
🌐
GitHub
github.com › openjdk-mirror › jdk7u-jdk › blob › master › src › share › classes › java › io › ByteArrayOutputStream.java
jdk7u-jdk/src/share/classes/java/io/ByteArrayOutputStream.java at master · openjdk-mirror/jdk7u-jdk
public ByteArrayOutputStream(int size) { if (size < 0) { throw new IllegalArgumentException("Negative initial size: " + size); } buf = new byte[size]; } · /** * Increases the capacity if necessary to ensure that it can hold ·
Author   openjdk-mirror
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › ByteArrayOutputStream.html
ByteArrayOutputStream (Java SE 11 & JDK 11 )
January 20, 2026 - Creates a new ByteArrayOutputStream, with a buffer capacity of the specified size, in bytes. ... IllegalArgumentException - if size is negative. ... Writes the specified byte to this ByteArrayOutputStream.
🌐
GeeksforGeeks
geeksforgeeks.org › java › bytearrayoutputstream-write-method-in-java-with-examples
ByteArrayOutputStream write() method in Java with Examples - GeeksforGeeks
May 28, 2020 - The write() method of ByteArrayOutputStream class in Java is used in two ways: 1. The write(int) method of ByteArrayOutputStream class in Java is used to write the specified byte to the ByteArrayOutputStream.
🌐
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 - Cеrtain subclassеs, likе FilеOutputStrеam or BytеArrayOutputStrеam, have built-in mеchanisms that allow us to retrieve the output bytes, such as in-mеmory buffеrs or a written file.
🌐
Blogger
codeinventions.blogspot.com › 2014 › 08 › creating-file-from-bytearrayoutputstrea.html
Code Inventions: Creating a file from ByteArrayOutputStream in Java.
August 25, 2014 - 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().
🌐
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)
Writes the entire contents of the specified input stream to this byte stream. Bytes from the input stream are read directly into the internal buffers of this streams. ... Return the current size of the byte array. ... Closing a ByteArrayOutputStream has no effect.