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 Overflow
🌐
Mkyong
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.
🌐
Java Guides
javaguides.net › 2023 › 09 › java-files-readallbytes.html
Java Files readAllBytes()
September 27, 2023 - The Files.readAllBytes() method reads all the bytes from a file. The method ensures that the file is closed when all bytes have been read or an I/O error, or other runtime error, occurs.
🌐
Attacomsian
attacomsian.com › blog › java-files-readallbytes-example
How to read a file using Files.readAllBytes() in Java
December 11, 2019 - try { // read all bytes byte[] bytes = Files.readAllBytes(Paths.get("input.txt")); // convert bytes to string String content = new String(bytes); // print contents System.out.println(content); } catch (IOException ex) { ex.printStackTrace(); ...
🌐
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
*/ @Test public void testTokenize_File() throws Exception { System.out.println("tokenize"); //java.net.URL url = this.class.getResource("test/resources/tokenizer/test-input.txt"); //File f=FileUtils.toFile(this.getClass().getResource("/tokenizer/test-input.txt.tokenized")); File f_in = new File(this.getClass().getResource("/tokenizer/test-input.txt").toURI()); File f_out = new File(this.getClass().getResource("/tokenizer/test-input.txt.tokenized").toURI()); String expResultString = new String(java.nio.file.Files.readAllBytes(f_out.toPath()), "UTF-8"); String inputString = new String(java.nio.file.Files.readAllBytes(f_in.toPath()), "UTF-8"); // tokenize without sentence splitting Tokenizer_PTB_Rulebased instance = new Tokenizer_PTB_Rulebased(false); String result = instance.tokenize(inputString); assertEquals(expResultString, result); //assertArrayEquals } Example 2 ·
Find elsewhere
🌐
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.
🌐
DEV Community
dev.to › tomaszs2 › java-files-readallbytes-4m6i
Java: Files.readAllBytes() - DEV Community
January 24, 2021 - Files.readAllBytes() reads all the bytes from a file #java.
🌐
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.
🌐
iO Flood
ioflood.com › blog › read-file-java
Read Files in Java: Guide to File and Reader Classes
February 20, 2024 - In this comprehensive guide, we’ve journeyed through the process of reading files in Java, starting from the basics and gradually moving towards more advanced techniques. We began with the basic use of readAllBytes() method, a simple yet powerful way to read small files.
🌐
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.
🌐
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[]? ...
🌐
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....