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. ... 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.
🌐
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.
Discussions

Java 1.8 and below equivalent for InputStream.readAllBytes() - Stack Overflow
Of course, you could design the method to close the resource but it must be documented to do so. I'd also expect a name more akin to readAllBytesAndClose. More on stackoverflow.com
🌐 stackoverflow.com
File to byte[] in Java - Stack Overflow
From JDK 7 you can use Files.readAllBytes(Path). ... Copyimport java.io.File; import java.nio.file.Files; File file; // ...(file is initialised)... More on stackoverflow.com
🌐 stackoverflow.com
java - Why is Files.readAllBytes() not accepting an encoding parameter? - Stack Overflow
Not sure I am understanding this correctly, but I was thinking that when I need to store a file content as a byte array in Java, I need to provide Java with the encoding of the file. If I want to ... More on stackoverflow.com
🌐 stackoverflow.com
java - Having trouble getting readAllBytes() method to work - Stack Overflow
Other students in my class have gotten this code to work, but I get an error. I am trying to use the readAllBytes() method for an assignment and I can't get it to work. My teacher there is another... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
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.dat")); // convert bytes to string String content = Arrays.toString(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.
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.io.inputstream.readallbytes
InputStream.ReadAllBytes Method (Java.IO) | Microsoft Learn
Java.IO · Assembly: Mono.Android.dll · Important · Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here. 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 - 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.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.nio.filenio.files.readallbytes
Files.ReadAllBytes(IPath) Method (Java.Nio.FileNio) | Microsoft Learn
Java.Nio.FileNio · Assembly: Mono.Android.dll · Important · Some information relates to prerelease product that may be substantially modified before it’s released. 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[]? ReadAllBytes(Java.Nio.FileNio.IPath?
🌐
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 - Applications that need to define a subclass of InputStream must always provide a method that returns the next byte of input · While the stream is open, the available(), read(), read(byte[]), read(byte[], int, int), readAllBytes(), readNBytes(byte[], int, int), readNBytes(int), skip(long), ...
🌐
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.
Top answer
1 of 2
6

Yes you are rather getting it wrong. But it easy to get it wrong ... if you don't understand the Java way of modeling textual data.

You are conflating the functionality of Reader / Writer with that of InputStream / OutputStream.

  • The former allow you to consume (read) and produce (write) characters; i.e. textual data represented using Unicode code-points.
  • The latter allow you to consume (read) and produce (write) bytes; i.e. binary or binary encoded data.

Now many Reader and Writer classes actually perform two functions at the same time; i.e. sourcing or sinking the data AND converting the data to / from characters. In some cases, you can tell the class how to do the conversion step by supplying an encoding.

By contrast an InputStream or OutputStream is a source or sink for bytes, and typically1 doesn't do any character translation.

In the case of Files.readAllBytes, the method is reading all of the bytes from a file and putting them into a byte array. It doesn't take a Charset parameter because it is not designed to do any decoding.

On the other hand, Files.readAllLines does take a Charset. That is because it is delivering the file content as an array of String objects representing the lines of the file. To when converting bytes to a Java String you need to say how the text / characters represented by the bytes are encoded.


1 - Hypothetically it could ... but you will see that Java doesn't provide classes like ReaderInputStream or WriterOutputStream. There is little need for them.

2 of 2
3

No, you only need a Charset if you want represent the bytes as a String (i.e. as text) because the meaning of the bytes depends on the encoding.

byte[] bytes = Files.readAllBytes(Paths.get("/temp.txt"));
String text = new String(bytes, StandardCharsets.ISO_8859_1);

A single byte array can represent multiple different strings depending on the encoding and the bytes in the array are independent of the encoding, so it doesn't make sense to be able to pass the encoding as an argument to this function.

🌐
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.
🌐
OpenJDK
bugs.openjdk.org › browse › JDK-8264777
[JDK-8264777] Overload optimized FileInputStream
@Param({ "100b.txt", "1k.txt", "10k.txt", "100k.txt", "1MB.txt", "10MB.txt" }) String fileName; java.io.File file; byte[] result; byte[] expected; @Setup public void setup() throws IOException { file = new java.io.File(dirName + fileName).getAbsoluteFile(); result = null; 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 void readAllBytesOld() throws IOException { try (java.io.InputStream input = new java.
🌐
Oracle
docs.oracle.com › en › java › javase › 16 › docs › api › java.base › java › io › ByteArrayInputStream.html
ByteArrayInputStream (Java SE 16 & JDK 16)
January 6, 2022 - readAllBytes() Reads all remaining bytes from the input stream. int · readNBytes​(byte[] b, int off, int len) Reads the requested number of bytes from the input stream into the given byte array. void · reset() Resets the buffer to the marked position. long · skip​(long n) Skips n bytes ...
🌐
GitHub
github.com › AdoptOpenJDK › openjdk-jdk11 › blob › master › test › jdk › java › io › InputStream › ReadAllBytes.java
openjdk-jdk11/test/jdk/java/io/InputStream/ReadAllBytes.java at master · AdoptOpenJDK/openjdk-jdk11
import java.util.Random; import jdk.test.lib.RandomFactory; · /* * @test · * @bug 8080835 8193832 · * @library /test/lib · * @build jdk.test.lib.RandomFactory · * @run main ReadAllBytes · * @summary Basic test for InputStream.readAllBytes · * @key randomness ·
Author   AdoptOpenJDK
🌐
Medium
34codefactory.medium.com › java-how-to-read-a-file-code-factory-9b3c6676e0f8
Java — How to read a File | Code Factory | by Code Factory | Medium
July 2, 2020 - If you want to read the entire contents of a file in a byte array then you can use the Files.readAllBytes() method. package com.example.java.programming.file;import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths;/** ...
🌐
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)