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.

Answer from Stephen C on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › Files.html
Files (Java Platform SE 8 )
April 21, 2026 - This method opens or creates a file in exactly the manner specified by the newByteChannel method with the exception that the READ option may not be present in the array of options. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present.
🌐
Mkyong
mkyong.com › home › java › java files.readallbytes example
Java Files.readAllBytes example
April 8, 2019 - package com.mkyong; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.Arrays; public class FileExample2 { public static void main(String[] args) { byte[] bytes = {1, 2, 3, 4, 5}; // Write into binary format try { Files.write(Paths.get("app.bin"), bytes, StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch (IOException x) { System.err.format("IOException: %s%n", x); } // Read try { byte[] content = Files.readAllBytes(Paths.get("app.bin")); // for binary System.out.println(Arrays.toString(content)); } catch (IOException e) { e.printStackTrace(); } } }
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.

🌐
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 - In Java 9, the readAllBytes() method reads all bytes from an InputStream object at once and blocks until all remaining bytes have been read and the end of the stream is detected, or an exception is thrown.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.io.file.readallbytes
File.ReadAllBytes(String) Method (System.IO) | Microsoft Learn
Opens a binary file, reads the contents of the file into a byte array, and then closes the file. public: static cli::array <System::Byte> ^ ReadAllBytes(System::String ^ path);
🌐
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 ...
Our example uses a new Files class ... or to check if a file is read-only. You can use Files.readAllBytes(Path) to read a complete file in memory....
🌐
GitHub
github.com › scala-native › scala-native › issues › 2755
java.nio.Files#readAllBytes opens a file and does not ...
July 26, 2022 - java.nio.Files#readAllBytes opens a file fcntl.O_RDONLY so it does not have the bug described in #2750, but it does have the bug of not checking the return from fcntl.open(). One would have to check the Java 8 documentation for readAllBytes to see what Exception should be returned.
Author   scala-native
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.nio.filenio.files.readallbytes
Files.ReadAllBytes(IPath) Method (Java.Nio.FileNio) | Microsoft Learn
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? path); [<Android.Runtime.Register("readAllBytes", "(Ljava/ni...
🌐
GeeksforGeeks
geeksforgeeks.org › c# › file-readallbytes-method-in-csharp-with-examples
File.ReadAllBytes() Method in C# with Examples - GeeksforGeeks
April 28, 2025 - // C# program to illustrate the usage // of File.ReadAllBytes(String) method // Using System and System.IO namespaces using System; using System.IO; class GFG { public static void Main() { // Specifying a file string path = @"file.txt"; // Calling the ReadAllBytes() function byte[] readText = File.ReadAllBytes(path); foreach(byte s in readText) { // Printing the binary array value of // the file contents Console.WriteLine(s); } } }
🌐
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.
🌐
Unity
docs.unity3d.com › 6000.2 › Documentation › ScriptReference › Windows.File.ReadAllBytes.html
Unity - Scripting API: Windows.File.ReadAllBytes
public static byte[] ReadAllBytes(string path); byte[] Managed array of bytes of the complete file content. Opens a binary file, reads the contents of the file into a byte array, and then closes the file. If the file is not found, the function ...
🌐
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(); }
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › nio › file › Files.html
Files (Java SE 11 & JDK 11 )
January 20, 2026 - This method opens or creates a file in exactly the manner specified by the newByteChannel method with the exception that the READ option may not be present in the array of options. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present.
🌐
Java Tips
javatips.net › api › java.nio.file.files.readallbytes
Java Examples for java.nio.file.Files.readAllBytes
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
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
🌐
Sipmann
sipmann.com › article › java › java - files.readallbytes throws outofmemory
Java - Files.readAllBytes throws OutOfMemory - Sipmann
March 9, 2018 - But be aware of the kinds of files your application will deal with because the Java API files have a limit for the buffer that is defined as Integer.MAX_VALUE as you can see above or at the OpenJDK sources. public static byte[] readAllBytes(Path path) throws IOException { try (FileChannel fc = FileChannel.open(path)) { long size = fc.size(); if (size > (long)Integer.MAX_VALUE) throw new OutOfMemoryError("Required array size too large"); byte[] arr = new byte[(int)size]; ByteBuffer bb = ByteBuffer.wrap(arr); while (bb.hasRemaining()) { if (fc.read(bb) < 0) { // truncated break; } } int nread = bb.position(); return (nread == size) ?