In Java, Files.readAllBytes(path) reads the whole file into a new byte[] and closes the file. It works for any kind of file: images, archives, or text.
It is the Java 7 way to load a file in one call. To turn the bytes into text, decode them with a charset: new String(bytes, StandardCharsets.UTF_8).
A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.readAllBytes(...), after import java.nio.file.Files;. You never create a Files object.
Syntax
Pass the path:
Java
byte[] data = Files.readAllBytes(path);
String text = new String(data, StandardCharsets.UTF_8);
Call
What it does
Files.readAllBytes(p)
All bytes as byte[].
Arrays.equals(a, b)
Compare two loaded files byte by byte.
b & 0xFF
Turn a signed byte into 0 to 255.
Since Java 7. Java bytes are signed, so 0xFF shows up as -1 in Arrays.toString.
Library declarations
Java
public static byte[] readAllBytes(Path path) throws IOException
Parameters
Parameter
Description
path
The file to read.
Return Value
Return Value
Description
byte[]
A new array with every byte of the file; its length equals the file size.
Example 1 — Read Four Bytes and Meet Signed Values
Save four raw bytes and read them back. The last one, 0xFF, prints as -1.
Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
public class ReadAllBytesSigned {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path bin = dir.resolve("data.bin");
Files.write(bin, new byte[] {1, 2, 3, (byte) 0xFF});
byte[] data = Files.readAllBytes(bin);
System.out.println("Length: " + data.length);
System.out.println("As Java bytes: " + Arrays.toString(data));
System.out.println("Last byte unsigned: " + (data[3] & 0xFF));
Files.delete(bin);
Files.delete(dir);
}
}
Output
Run with java ReadAllBytesSigned. The output is the same on Windows, macOS, and Linux.
Length: 4
As Java bytes: [1, 2, 3, -1]
Last byte unsigned: 255
How It Works
Length is the file size. Four bytes were written, so the array has four elements.
Signed bytes. Java's byte ranges from -128 to 127, so the bit pattern 0xFF is shown as -1.
Unsigned view.data[3] & 0xFF widens the byte to an int and masks it, giving the familiar 255.
Example 2 — Do Two Files Hold the Same Bytes?
Load files and compare their content with Arrays.equals, unlike isSameFile, which compares identity.
Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
public class ReadAllBytesCompare {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path a = dir.resolve("a.txt");
Path b = dir.resolve("b.txt");
Path c = dir.resolve("c.txt");
Files.write(a, "same words".getBytes(StandardCharsets.UTF_8));
Files.copy(a, b);
Files.write(c, "same Words".getBytes(StandardCharsets.UTF_8));
byte[] bytesA = Files.readAllBytes(a);
System.out.println("a vs b: " + Arrays.equals(bytesA, Files.readAllBytes(b)));
System.out.println("a vs c: " + Arrays.equals(bytesA, Files.readAllBytes(c)));
Files.delete(a);
Files.delete(b);
Files.delete(c);
Files.delete(dir);
}
}
Output
Run with java ReadAllBytesCompare. The output is the same on Windows, macOS, and Linux.
a vs b: true
a vs c: false
How It Works
An exact copy. b.txt is made with Files.copy, so its bytes match a.txt.
One letter differs. c.txt has a capital W, a different byte value.
Content comparison.Arrays.equals checks length and every element, so only the copy matches. Java 12 added Files.mismatch for the same job without loading both files.
Example 3 — Recognise a PNG by Its First Bytes
Many formats start with fixed "magic" bytes. Load the file and print the first four bytes in hex.
Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class ReadAllBytesSignature {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path img = dir.resolve("logo.png");
Files.write(img, new byte[] {(byte) 0x89, 'P', 'N', 'G', 13, 10, 26, 10});
byte[] head = Files.readAllBytes(img);
StringBuilder hex = new StringBuilder();
for (int i = 0; i < 4; i++) {
hex.append(String.format("%02X ", head[i] & 0xFF));
}
boolean png = (head[0] & 0xFF) == 0x89 && head[1] == 'P' && head[2] == 'N' && head[3] == 'G';
System.out.println("First bytes: " + hex.toString().trim());
System.out.println("Looks like PNG: " + png);
Files.delete(img);
Files.delete(dir);
}
}
Output
Run with java ReadAllBytesSignature. The output is the same on Windows, macOS, and Linux.
First bytes: 89 50 4E 47
Looks like PNG: true
How It Works
A fake but valid signature. The eight bytes written are the real PNG header: 0x89, the letters PNG, then carriage return, line feed, 0x1A, and line feed.
Hex formatting.%02X prints each unsigned byte as two upper-case hex digits; P, N, and G are 0x50, 0x4E, and 0x47.
Content beats the name. Checking bytes is more reliable than trusting the .png extension, which anyone can rename.
Frequently Asked Questions
Call byte[] data = Files.readAllBytes(path). It reads the whole file and closes it.
Java bytes are signed. Use b & 0xFF to get the unsigned value from 0 to 255.
About 2 GB, the largest possible array, and in practice whatever fits in the JVM heap. Larger files cause OutOfMemoryError.
new String(Files.readAllBytes(path), StandardCharsets.UTF_8). Java 11 added Files.readString for this.
For small files, compare Files.readAllBytes of both with Arrays.equals. Java 12 and later also provide Files.mismatch.