You can use Apache Commons IO to handle this and similar tasks.

The IOUtils type has a static method to read an InputStream and return a byte[].

InputStream is;
byte[] bytes = IOUtils.toByteArray(is);

Internally this creates a ByteArrayOutputStream and copies the bytes to the output, then calls toByteArray(). It handles large files by copying the bytes in blocks of 4KiB.

Answer from Rich Seller on Stack Overflow
🌐
Coderanch
coderanch.com › t › 275789 › java › Convert-java-io-File-ByteArryInputStream
Convert java.io.File to ByteArryInputStream? (I/O and Streams forum at Coderanch)
How can I convert a java.io.File to a ByteArrayInputStream? Many Thanks. ... Why would you want to do that? A ByteArrayInputStream doesn't seem to have any methods that any other InputStream doesn't have. It just has the additional ability (or restriction in this case) that it reads input from an existing byte array.
🌐
Baeldung
baeldung.com › home › java › java io › java inputstream to byte array and bytebuffer
Java InputStream to Byte Array and ByteBuffer | Baeldung
January 8, 2024 - This article illustrated various ways to convert a raw input stream to a byte array and a ByteBuffer using plain Java, Guava, and Apache Commons IO.
🌐
Baeldung
baeldung.com › home › java › java io › java byte array to inputstream
Java Byte Array to InputStream | Baeldung
December 7, 2023 - This article is part of the “Java – Back to Basic” series here on Baeldung. ... @Test public void givenUsingPlainJava_whenConvertingByteArrayToInputStream_thenCorrect() throws IOException { byte[] initialArray = { 0, 1, 2 }; InputStream targetStream = new ByteArrayInputStream(initialArray); } Next – let’s use wrap the byte array into the Guava ByteSource – which then allows us to get the stream:
🌐
Blogger
javarevisited.blogspot.com › 2014 › 04 › how-to-convert-byte-array-to-inputstream-outputstream-java-example.html
How to Convert Byte Array to InputStream and OutputStream in Java? Example
June 11, 2025 - Are you stuck with your coding because you have a byte array and the next method in the chain needs an InputStream? don't worry Java has a solution for that, You can use ByteArrayInputStream to convert byte array to InputStream in Java.
🌐
Programiz
programiz.com › java-programming › bytearrayinputstream
Java ByteArrayInputStream (With Examples)
Java FileInputStream Class · Java BufferedInputStream Class · Java InputStreamReader Class · Java ByteArrayOutputStream Class · Java Reader Class · The ByteArrayInputStream class of the java.io package can be used to read an array of input data (in bytes).
🌐
GeeksforGeeks
geeksforgeeks.org › java › convert-byte-array-to-file-using-java
Convert byte[] array to File using Java - GeeksforGeeks
July 11, 2025 - ... // Java Program to convert ... exceptions try { // Initialize a pointer in file // using OutputStream OutputStream os = new FileOutputStream(file); // Starting writ...
Find elsewhere
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › io › fileinputstream
Read file in byte array with FileInputStream - Java Code Geeks
October 26, 2013 - Create a String from the byte array. Don’t forget to close the FileInputStream, using the close() API method. Let’s take a look at the code snippet that follows: package com.javacodegeeks.snippets.core; import java.io.File; import ...
🌐
Coderanch
coderanch.com › t › 277921 › java › ByteArrayInputstream-file
ByteArrayInputstream to a file (I/O and Streams forum at Coderanch)
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... hi , i find out way ByteArrayInputStream byteArrayInputStream = sysObj.getContent(); String outStr=byteArrayInputStream.toString(); File newFile=new File("somefilname"); FileOutputStream fos = new FileOutputStream(newFile); int data; while((data=byteArrayInputStream.read())!=-1) { char ch = (char)data; fos.write(ch); } fos.flush(); fos.close(); thanks
🌐
Oracle
forums.oracle.com › ords › apexds › post › bytearrayinputstream-to-fileinputstream-3325
ByteArrayInputStream to FileInputStream - Oracle Forums
February 18, 2009 - I am not very experienced in Java and I am having trouble converting a ByteArrayInputStream object to a FileInputStream object. There is a long story behind this so I won't waste anyone's time with it...
🌐
Medium
medium.com › @lavishj77 › java-i-o-byte-stream-implementation-6acf5a9ec848
Java I/O Byte Stream Implementation | by Lavish Jain | Medium
April 16, 2022 - Similarly, BufferedOutputStream is used to capture bytes in a buffer, and write the whole buffer in one batch to an underlying OutputStream for increased performance. Except for adding buffering to your input streams, BufferedInputStream and BufferedOutputStream behaves exactly like an InputStream and OutputStream respectively having same implementations of read() and write() methods. InputStream bos = new BufferedInputStream( new FileInputStream("inputFile.txt")); OutputStream output = new BufferedOutputStream( new FileOutputStream("outputFile.txt"));//to add custom buffer size int bufferSize = 8 * 1024; InputStream bos = new BufferedInputStream( new FileInputStream("inputFile.txt"),bufferSize); OutputStream output = new BufferedOutputStream( new FileOutputStream("outputFile.txt"),bufferSize);
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › io › ByteArrayInputStream.html
ByteArrayInputStream (Java Platform SE 7 )
Reads up to len bytes of data into an array of bytes from this input stream. If pos equals count, then -1 is returned to indicate end of file. Otherwise, the number k of bytes read is equal to the smaller of len and count-pos. If k is positive, then bytes buf[pos] through buf[pos+k-1] are copied ...
🌐
How to do in Java
howtodoinjava.com › home › i/o › read file to byte[] in java
Read File to Byte[] in Java
December 14, 2022 - Path path = Paths.get("C:/temp/test.txt"); byte[] data = Files.readAllBytes(path); Use FileInputStream for reading the content of a file when you already have the InputStream reference.
🌐
Jenkov
jenkov.com › tutorials › java-io › bytearrayinputstream.html
Java ByteArrayInputStream
November 15, 2019 - In other words, the ByteArrayInputStream class can turn a byte array into an InputStream. The ByteArrayInputStream class is a subclass of the InputStream class, so you can use a ByteArrayInputStream as an InputStream.
🌐
Mkyong
mkyong.com › home › java › java – how to convert file to byte[]
Java - How to convert File to byte[] - Mkyong.com
September 17, 2020 - private static void readFileToBytes(String filePath) throws IOException { File file = new File(filePath); byte[] bytes = new byte[(int) file.length()]; // funny, if can use Java 7, please uses Files.readAllBytes(path) try(FileInputStream fis = new FileInputStream(file)){ fis.read(bytes); } }
🌐
TutorialsPoint
tutorialspoint.com › convert-byte-array-to-file-using-java
Convert byte[] array to File using Java
June 17, 2024 - In this possible approach, we are going to apply the java.io.FileOutputStream method on an byte[] array list.