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
🌐
Java Guides
javaguides.net › 2023 › 09 › java-files-readallbytes.html
Java Files readAllBytes()
September 27, 2023 - In this guide, you will learn about the Files readAllBytes() method in Java programming and how to use it with an example.
🌐
Mkyong
mkyong.com › home › java › java files.readallbytes example
Java Files.readAllBytes example - Mkyong.com
April 8, 2019 - package com.mkyong.calculator; import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; import java.util.List; public class FileExample1 { public static void main(String[] args) { Charset utf8 = StandardCharsets.UTF_8; List<String> list = Arrays.asList("Line 1", "Line 2"); // Write try { Files.write(Paths.get("app.log"), list, utf8, StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch (IOException x) { System.err.format("IOException: %s%n", x); } // Read try { byte[] content = Files.readAllBytes(Paths.get("app.log")); System.out.println(new String(content)); // for binary //System.out.println(Arrays.toString(content)); } catch (IOException e) { e.printStackTrace(); } } } Output ·
🌐
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 - The Files.readAllBytes() is the best method for using Java 7, 8 and above. It reads all bytes from a file and closes the file. The file is also closed on an I/O error or another runtime exception is thrown.
🌐
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.
🌐
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.
🌐
Mkyong
mkyong.com › home › java › how to read a file in java
How to read a file in Java - Mkyong.com
July 17, 2020 - Files.readAllBytes, returns a byte[] (Java 7), max file size 2G. Files.readAllLines, returns a List<String> (Java 8)
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-inputstream-to-byte-array-in-java
How to Convert InputStream to Byte Array in Java? - GeeksforGeeks
July 23, 2025 - // Java Program to Convert InputStream to Byte Array // Using read(byte[]) or readAllBytes() // Importing required classes import java.io.*; import java.nio.charset.StandardCharsets; // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating a Input Stream here // Usually it comes from files, programs InputStream inputStream = new ByteArrayInputStream( "GeeksForGeeks".getBytes( StandardCharsets.UTF_8)); // Taking the InputStream data into a byte array byte[] byteArray = null; // Try block to check for exceptions try { byteArray = inputStream.readAllBytes(); } // Catch block to handle the exceptions catch (IOException e) { // Print and display the exceptions System.out.println(e); } // Iterating over using for each loop for (byte b : byteArray) // Printing the content of byte array System.out.print((char)b + " "); } } Output ·
Find elsewhere
🌐
Blogger
javarevisited.blogspot.com › 2015 › 02 › how-to-read-file-in-one-line-java-8.html
How to read a File in One Line in Java? Files.readAllLines() Example Tutorial
Since Files.lines() is only available in Java 8 and main reason of using it to take advantage of lazily loaded stream API, I am expecting them to closing the file once it read. ... Anonymous said... Quick question about ReadAllBytes() - I converted a file into byte array and stored in the ...
🌐
How to do in Java
howtodoinjava.com › home › i/o › java read file to string (with examples)
Java Read File to String (with Examples)
October 22, 2023 - Path filePath = Path.of("c:/te... fileContent = contentBuilder.toString(); The readAllBytes() method reads all the bytes from a file into a byte[]. Do not use this method for reading large files....
🌐
OpenJDK
bugs.openjdk.org › browse › JDK-8264777
[JDK-8264777] Overload optimized FileInputStream
@Param({ "100b.txt", "1k.txt", ... expected = java.nio.file.Files.readAllBytes(file.toPath()); } @TearDown public void check() { assert Arrays.equals(expected, result) : "Nothing changed?"; } public static final int MAX_ARRAY_LENGTH = Integer.MAX_VALUE - 8; @Benchmark public ...
🌐
Oracle
forums.oracle.com › ords › apexds › post › why-when-i-use-files-readallbytes-in-java-1-8-got-error-7457
Why when I use Files.readAllBytes in Java 1.8, got error - Oracle Forums
June 7, 2022 - For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you
🌐
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 - As we can see, the Files class comes with the readAllBytes() method which returns all the bytes from the specified path file.
🌐
Medium
medium.com › @alxkm › how-to-read-a-file-into-a-string-in-java-multiple-approaches-6c781a1c3cbd
Java Interview: How to Read a File into a String in Java-Multiple Approaches | by Alex Klimenko | Medium
August 9, 2025 - If you're on Java 7 or 8, this is a clean alternative. import java.nio.file.Files; import java.nio.file.Paths; byte[] bytes = Files.readAllBytes(Paths.get("path/to/file.txt")); String content = new String(bytes, StandardCharsets.UTF_8); Works ...
🌐
Adam-bien
adam-bien.com › roller › abien › entry › java_8_reading_a_file
Java 8: Reading A File Into A String
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public static void main(String[] args) throws IOException { String content = new String(Files.readAllBytes(Paths.get("duke.java"))); } Enjoy Java 8!
🌐
GitHub
github.com › adoptium › bumblebench › issues › 27
NoSuchMethodException: sun.misc.IOUtils.readAllBytes on java 8 · Issue #27 · adoptium/bumblebench
October 15, 2020 - java -jar BumbleBench.jar ...hodException: sun.misc.IOUtils.readAllBytes(java.io.InputStream) at java.lang.Class.getDeclaredMethod(Class.java:2130) at net.adoptopenjdk.bumblebench.core.Util.<clinit>(Util.java:36) ... openjdk version "1.8.0_222" OpenJDK Runtime Environment ...
Author   adoptium
🌐
Baeldung
baeldung.com › home › java › java io › java inputstream to string
Java InputStream to String | Baeldung
January 5, 2024 - If we’re on Java 9 or above, we can utilize a new readAllBytes method added to the InputStream: @Test public void givenUsingJava9_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { String originalString = randomAlphabetic(DEFAULT_SIZE); InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); String text = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); assertThat(text, equalTo(originalString)); } We need to be aware that this simple code is intended for simple cases where it’s convenient to read all bytes into a byte array.
🌐
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 - import java.nio.*; import java.nio.file.*; import java.io.*; import java.util.stream.*; import java.nio.charset.StandardCharsets; public class ReadAllBytesMethodTest { public static void main(String args[]) { try(InputStream stream = Files.newInputStream(Paths.get("C://Temp//Technology.txt"))) { // Convert stream to string String contents = new String(stream.readAllBytes(), StandardCharsets.UTF_8); // To print the string content System.out.println(contents); } catch(IOException ioe) { ioe.printStackTrace(); } } } We have created a "Technology.txt" file in a "C:\Temp" folder with simple data: { "JAVA", "PYTHON", "JAVASCRIPT", "SELENIUM", "SCALA"}. "JAVA", "PYTHON", "JAVASCRIPT", "SELENIUM", "SCALA" Alshifa Hasnain ·
🌐
iO Flood
ioflood.com › blog › read-file-java
Read Files in Java: Guide to File and Reader Classes
February 20, 2024 - In this example, the readAllBytes() method is enclosed in a try block. If an IOException occurs, the catch block catches it and prints the stack trace. Files can be encoded in different formats, and reading a file with the wrong encoding can result in garbled output. When you’re reading a file, it’s important to know its encoding and use the correct charset when converting bytes to a string. Here’s an example of reading a file with UTF-8 ...