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
🌐
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 - If our destination file or any of the parent directories don’t exist, they’ll be created. In this brief article, we learned how to write binary data from a byte[] to a file using plain Java and two popular Java utility libraries, Google Guava and Apache Commons IO.
🌐
Tutorialspoint
tutorialspoint.com › java › io › bytearrayoutputstream_writeto.htm
Java - ByteArrayOutputStream writeTo(OutputStream out) method
Using writeTo(OutputStream out)− The writeTo() method is called to transfer the data from the ByteArrayOutputStream to the FileOutputStream.
🌐
Programiz
programiz.com › java-programming › bytearrayoutputstream
Java ByteArrayOutputStream (With Examples)
ByteArrayOutputStream output = new ByteArrayOutputStream(); To write the data to the output stream, we have used the write() method.
🌐
GeeksforGeeks
geeksforgeeks.org › java › bytearrayoutputstream-write-method-in-java-with-examples
ByteArrayOutputStream write() method in Java with Examples - GeeksforGeeks
May 28, 2020 - length - It represents the number of bytes to be written. Return value: The method does not return any value. Exceptions: This method does not throw any exception. Below program illustrates write(byte[ ], int, int) method in ByteArrayOutputStream class in IO package: Program:
🌐
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.
Find elsewhere
🌐
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.
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 1388237 › handle-the-bytearrayoutputstream-for-large-image-s
Handle the "ByteArrayOutputStream" for large image size more than 2GB while download/uploading data from blob storage - Microsoft Q&A
From my application I am trying to upload/download large size images(specifically more than 2GB files) and we have used ByteArrayOutputStream uploadImgFile = new ByteArrayOutputStream(); Problem with the ByteArrayOutputSteam is it can hold up ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › › › index.html
ByteArrayOutputStream (Java Platform SE 8 )
JavaScript is disabled on your browser · Frame Alert · This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client. Link to Non-frame version
🌐
YouTube
youtube.com › watch
Java Tutorial #76 - Java FileOutputStream Class Examples | Write in File - YouTube
Java Tutorial #76 - Java File Output Stream Class Examples | FileOutputStream to write in File (File Handling)In this video by Programming for Beginners we w...
Published   September 12, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › java › convert-byte-array-to-file-using-java
Convert byte[] array to File using Java - GeeksforGeeks
July 11, 2025 - In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java tutorial › java bytearrayoutputstream
Java ByteArrayOutputStream | Examples of Java ByteArrayOutputStream
April 10, 2023 - Java program to copy the content of a text file to another file. ... import java.io.*; //class public class ByteExample { //main method public static void main(String args[])throws Exception { //create fileoutputstreams 1 and 2 FileOutputStream fobj1=new FileOutputStream("F:\\EduCBA\\May\\byte1.txt"); FileOutputStream fobj2=new FileOutputStream("F:\\EduCBA\\May\\byte2.txt"); //create bytearrayoutputstream ByteArrayOutputStream bobj=new ByteArrayOutputStream(); //write the content bobj.write(100); //write to the text files bobj.writeTo(fobj1); bobj.writeTo(fobj2); bobj.flush() ; // bobj.close() ; System.out.println("Operation runs successfully..."); } }
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Baeldung
baeldung.com › home › java › java io › guide to java outputstream
Guide to Java OutputStream | Baeldung
December 3, 2025 - We can use this method to write one specific byte to the OutputStream. Since the argument “int” comprises four bytes, as par the contract only the first low order byte is written and the remaining three high order bytes and ignored: public static void fileOutputStreamByteSingle(String file, String data) throws IOException { byte[] bytes = data.getBytes(); try (OutputStream out = new FileOutputStream(file)) { out.write(bytes[6]); } }
🌐
TutorialsPoint
tutorialspoint.com › convert-byte-array-to-file-using-java
Convert byte[] array to File using Java
June 17, 2024 - Step 11 Get the return value as a file. Step 12 Terminate the process. In this possible algorithm, we are going to show you how to perform a conversion process on a byte() array node to make it a file.
🌐
Free Support Forum
forum.groupdocs.com › groupdocs.conversion product family
How to convert a file without physically storing it on a disk - Free Support Forum - groupdocs.com
December 11, 2023 - We are also checking Group docs conversion API capability to convert word/image to pdf. We want to directly convert the file without storing the file physically. I have written the following java code to directly convert byte[] (.docx) to pdf byte[] using the reference given in the help. public byte[] convertFile(byte[] input) throws IOException { ByteArrayOutputStream byteStream = null; Supplier new ByteArrayInputStream(input); SaveDocument...
🌐
Medium
medium.com › @lavishj77 › java-i-o-byte-stream-implementation-6acf5a9ec848
Java I/O Byte Stream Implementation | by Lavish Jain | Medium
April 16, 2022 - Invoking the flush() method guarantees that the last of the data you thought you had already written actually gets out to the file.
🌐
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 - package com.mkyong.io.howto; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class ByteToFile { public static void main(String[] args) { try { // tested with character data and binary data // file to bytes[] byte[] bytes = Files.readAllBytes(Paths.get("/home/mkyong/test/file.txt")); // save byte[] to a file writeBytesToFile("/home/mkyong/test/file2.txt", bytes); writeBytesToFileNio("/home/mkyong/test/file3.txt", bytes); writeBytesTo