Here's one way to solve this without relying on third-party libraries:
inputStream.reset();
byte[] bytes = new byte[inputStream.available()];
DataInputStream dataInputStream = new DataInputStream(inputStream);
dataInputStream.readFully(bytes);
Or if you don't mind using thirdparties (Commons IO):
byte[] bytes = IOUtils.toByteArray(is);
Guava also helps:
byte[] bytes = ByteStreams.toByteArray(inputStream);
Answer from Mark Bramnik on Stack OverflowMkyong
mkyong.com › home › java › java files.readallbytes example
Java Files.readAllBytes example - Mkyong.com
April 8, 2019 - byte[] content = Files.readAllBytes(Paths.get("app.log")); System.out.println(new String(content)); A Java example to write and read a normal text file.
TutorialsPoint
tutorialspoint.com › article › when-to-use-the-readallbytes-method-of-inputstream-in-java-9
When to use the readAllBytes() method of InputStream in Java 9?
June 12, 2025 - We can use the readAllBytes() method from the InputStream class to read all bytes into a byte array. The Java InputStream class is the superclass of all classes representing an input stream of bytes.
Top answer 1 of 2
15
Here's one way to solve this without relying on third-party libraries:
inputStream.reset();
byte[] bytes = new byte[inputStream.available()];
DataInputStream dataInputStream = new DataInputStream(inputStream);
dataInputStream.readFully(bytes);
Or if you don't mind using thirdparties (Commons IO):
byte[] bytes = IOUtils.toByteArray(is);
Guava also helps:
byte[] bytes = ByteStreams.toByteArray(inputStream);
2 of 2
9
You can use the good old read method like this:
public static byte[] readAllBytes(InputStream inputStream) throws IOException {
final int bufLen = 1024;
byte[] buf = new byte[bufLen];
int readLen;
IOException exception = null;
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
while ((readLen = inputStream.read(buf, 0, bufLen)) != -1)
outputStream.write(buf, 0, readLen);
return outputStream.toByteArray();
} catch (IOException e) {
exception = e;
throw e;
} finally {
if (exception == null) inputStream.close();
else try {
inputStream.close();
} catch (IOException e) {
exception.addSuppressed(e);
}
}
}
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
Java Tips
javatips.net › api › java.nio.file.files.readallbytes
Java Examples for java.nio.file.Files.readAllBytes - Javatips.net
public static void main(String[] args) throws Exception { // get reference of source/input file java.nio.file.Path path = java.nio.file.Paths.get("input.pdf"); // read all the contents from source file into ByteArray byte[] data = java.nio.file.Files.readAllBytes(path); // create an instance of Stream object from ByteArray contents InputStream is = new ByteArrayInputStream(data); // Instantiate Document object from stream instance Document pdfDocument = new Document(is); // setup new file to be added as attachment FileSpecification fileSpecification = new FileSpecification("test.txt", "Sample text file"); // Specify Encoding property setting it to FileEncoding.None fileSpecification.setEncoding(FileEncoding.None); // add attachment to document's attachment collection pdfDocument.getEmbeddedFiles().add(fileSpecification); // save new output pdfDocument.save("output.pdf"); }
Oracle
docs.oracle.com › javase › 9 › docs › api › java › io › InputStream.html
InputStream (Java SE 9 & JDK 9 )
public byte[] readAllBytes() throws IOException · Reads all remaining bytes from the input stream. This method blocks until all remaining bytes have been read and end of stream is detected, or an exception is thrown. This method does not close the input stream.
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.io.inputstream.readallbytes
InputStream.ReadAllBytes Method (Java.IO) | Microsoft Learn
Reads all remaining bytes from the input stream. [Android.Runtime.Register("readAllBytes", "()[B", "GetReadAllBytesHandler", ApiSince=33)] public virtual byte[]? ReadAllBytes(); [<Android.Runtime.Register("readAllBytes", "()[B", "GetReadAllBytesHandler", ApiSince=33)>] abstract member ReadAllBytes : unit -> byte[] override this.ReadAllBytes : unit -> byte[] Byte[] a byte array containing the bytes read from this input stream ...
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 - In Java, reading a file to byte array may be needed in various situations. For example, passing the information through the network and other APIs for further processing. Let’s learn about a few ways of reading data from files into a byte array in Java. The Files.readAllBytes() is the best method for using Java 7, 8 and above.
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.nio.filenio.files.readallbytes
Files.ReadAllBytes(IPath) Method (Java.Nio.FileNio) | Microsoft Learn
Microsoft makes no warranties, express or implied, with respect to the information provided here. Reads all the bytes from a file. [Android.Runtime.Register("readAllBytes", "(Ljava/nio/file/Path;)[B", "", ApiSince=26)] public static byte[]? ...
Top answer 1 of 16
1517
From JDK 7 you can use Files.readAllBytes(Path).
Example:
import java.io.File;
import java.nio.file.Files;
File file;
// ...(file is initialised)...
byte[] fileContent = Files.readAllBytes(file.toPath());
2 of 16
584
It depends on what best means for you. Productivity wise, don't reinvent the wheel and use Apache Commons. Which is here FileUtils.readFileToByteArray(File input).
Linux Hint
linuxhint.com › java-input-stream-read-all-bytes
Linux Hint – Linux Hint
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › InputStream.html
InputStream (Java SE 11 & JDK 11 )
January 20, 2026 - While the stream is open, the available(), read(), read(byte[]), read(byte[], int, int), readAllBytes(), readNBytes(byte[], int, int), readNBytes(int), skip(long), and transferTo() methods all behave as if end of stream has been reached.
Stack Overflow
stackoverflow.com › questions › 63855541 › having-trouble-getting-readallbytes-method-to-work
java - Having trouble getting readAllBytes() method to work - Stack Overflow
I am trying to use the readAllBytes() method for an assignment and I can't get it to work. My teacher there is another way he intended it to work using That method is not defined for DataInputStream. Take a look at the read(byte[]) method and the first integer in the datastream shows how large the byte Array is. Either way will work, I just am getting an error on line 39, and if I delete "import java.io.DataInputStream;" it fixes that error but I get an error on line 31 instead.
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 readFileTo...7</version> </dependency> The below example uses the NIO Files.readAllBytes to read an image file into a byte[], and Files.write to save the byte[] into a new image file....