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
🌐
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
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.
🌐
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.
🌐
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 ...
Find elsewhere
🌐
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 - In this quick tutorial, we’re going to take a look at how to convert an InputStream to a byte[] and ByteBuffer – first using plain Java, then using Guava and Commons IO.
🌐
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...
Top answer
1 of 8
213

You create and use byte array I/O streams as follows:

byte[] source = ...;
ByteArrayInputStream bis = new ByteArrayInputStream(source);
// read bytes from bis ...

ByteArrayOutputStream bos = new ByteArrayOutputStream();
// write bytes to bos ...
byte[] sink = bos.toByteArray();

Assuming that you are using a JDBC driver that implements the standard JDBC Blob interface (not all do), you can also connect a InputStream or OutputStream to a blob using the getBinaryStream and setBinaryStream methods1, and you can also get and set the bytes directly.

(In general, you should take appropriate steps to handle any exceptions, and close streams. However, closing bis and bos in the example above is unnecessary, since they aren't associated with any external resources; e.g. file descriptors, sockets, database connections.)

1 - The setBinaryStream method is really a getter. Go figure.

2 of 8
14

I'm assuming you mean that 'use' means read, but what i'll explain for the read case can be basically reversed for the write case.

so you end up with a byte[]. this could represent any kind of data which may need special types of conversions (character, encrypted, etc). let's pretend you want to write this data as is to a file.

firstly you could create a ByteArrayInputStream which is basically a mechanism to supply the bytes to something in sequence.

then you could create a FileOutputStream for the file you want to create. there are many types of InputStreams and OutputStreams for different data sources and destinations.

lastly you would write the InputStream to the OutputStream. in this case, the array of bytes would be sent in sequence to the FileOutputStream for writing. For this i recommend using IOUtils

byte[] bytes = ...;//
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
FileOutputStream out = new FileOutputStream(new File(...));
IOUtils.copy(in, out);
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);

and in reverse

FileInputStream in = new FileInputStream(new File(...));
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(in, out);
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
byte[] bytes = out.toByteArray();

if you use the above code snippets you'll need to handle exceptions and i recommend you do the 'closes' in a finally block.

🌐
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
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › convert-byte-array-to-file-using-java
Convert byte[] array to File using Java - GeeksforGeeks
July 11, 2025 - Implementation: Convert a String into a byte array and write it in a file. ... // Java Program to convert Byte Array to File // Importing required classes import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; // ...
🌐
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.
🌐
Baeldung
baeldung.com › home › java › java io › convert file to byte array in java
Convert File to Byte Array in Java | Baeldung
January 5, 2024 - Here, we created an instance of the FileInputStream class using the given sample.txt file. Furthermore, we invoked the read(byte[] b) method to read the data from the FileInputStream instance into the defined array of bytes.
🌐
Jenkov
jenkov.com › tutorials › java-io › fileinputstream.html
Java FileInputStream
August 28, 2019 - The BufferedInputStream reads a chunk of bytes into a byte array from the underlying FileInputStream. You can then read the bytes one by one from the BufferedInputStream and still get a lot of the speedup that comes from reading an array of ...