Use the FileUtils#readFileToByteArray(File) from Apache Commons IO, and then create the ByteArrayInputStream using the ByteArrayInputStream(byte[]) constructor.

public static ByteArrayInputStream retrieveByteArrayInputStream(File file) {
    return new ByteArrayInputStream(FileUtils.readFileToByteArray(file));
}
Answer from npe on Stack Overflow
🌐
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
🌐
Tutorialspoint
tutorialspoint.com › java › java_bytearrayinputstream.htm
Java - ByteArrayInputStream
Java Vs. C++ ... The ByteArrayInputStream class allows a buffer in the memory to be used as an InputStream. The input source is a byte array. ByteArrayInputStream class provides the following constructors.
🌐
Medium
medium.com › @lavishj77 › java-i-o-byte-stream-implementation-6acf5a9ec848
Java I/O Byte Stream Implementation | by Lavish Jain | Medium
April 16, 2022 - File I/O, either reading it or writing uses expensive and limited operating system resources, and so when you’re done, invoking close() will free up those resources. 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.
🌐
How to do in Java
howtodoinjava.com › home › i/o › writing byte[] to a file in java
Writing Byte[] to a File in Java
April 18, 2022 - Do not forget to close the output ... FileOutputStream(file)) { os.write(bytes); } The FileUtils class has method writeByteArrayToFile() that writes the byte array data into the specified file....
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › io › ByteArrayInputStream.html
ByteArrayInputStream (Java SE 21 & JDK 21)
July 15, 2025 - 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 ...
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java io › writing byte[] to a file in java
Writing byte[] to a File in Java | Baeldung
May 13, 2026 - We’ll open an output stream to our destination file, and then we can simply pass our byte[] dataForWriting to the write method. Note that we’re using a try-with-resources block here to ensure that we close the OutputStream in case an IOException is thrown. The Java NIO package was introduced in Java 1.4, and the file system API for NIO was introduced as an extension in Java 7.
🌐
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 ... an InputStream to a byte[] and ByteBuffer – first using plain Java, then using Guava and Commons IO. This article is part of the “Java – Back to Basic” series here on Baeldung. Discover Spring's StreamUtils class. ... We learn how to serialize and deserialize objects in Java. ... Quick and practical examples focused on converting String objects to different data types in Java. ... Let’s look at obtaining a byte array from simple ...
🌐
Programiz
programiz.com › java-programming › bytearrayinputstream
Java ByteArrayInputStream (With Examples)
In this tutorial, we will learn about Java ByteArrayInputStream and its methods with the help of examples to read an array of input data.
🌐
Jenkov
jenkov.com › tutorials › java-io › bytearrayinputstream.html
Java ByteArrayInputStream
November 15, 2019 - The Java ByteArrayInputStream can be handy if your data is stored in an array, but you have a component that can only process it as an InputStream. The ByteArrayInputStream can thus wrap the byte array, and turn it into a stream.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › ByteArrayInputStream.html
ByteArrayInputStream (Java Platform SE 8 )
April 21, 2026 - 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 ...
🌐
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.
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.

🌐
Javatpoint
javatpoint.com › java-bytearrayinputstream-class
Java ByteArrayInputStream Class - javatpoint
Java ByteArrayInputStream Class for beginners and professionals with examples on Java IO or Input Output in Java with input stream, output stream, reader and writer class. The java.io package provides api to reading and writing data.
🌐
Programiz
programiz.com › java-programming › bytearrayoutputstream
Java ByteArrayOutputStream (With Examples)
Here, the size specifies the length of the array. The ByteArrayOutputStream class provides the implementation of the different methods present in the OutputStream class. write(int byte) - writes the specified byte to the output stream
🌐
W3Docs
w3docs.com › java
Convert InputStream to byte array in Java
Here is an example of how you can use the readAllBytes() method to convert an InputStream to a byte array in Java: import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Paths; public class InputStreamToByteArrayExample ...