Java Files readAllBytes() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

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);
CallWhat it does
Files.readAllBytes(p)All bytes as byte[].
Arrays.equals(a, b)Compare two loaded files byte by byte.
b & 0xFFTurn 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

ParameterDescription
pathThe file to read.

Return Value

Return ValueDescription
byte[]A new array with every byte of the file; its length equals the file size.

Compare with Similar Methods

Loading data:

MethodHow it differs
readString()Java 11; bytes already decoded to text.
newInputStream()Read gradually instead of all at once.
write()The reverse: save a byte[].

Java Examples

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);
    }
}

How It Works

  1. Length is the file size. Four bytes were written, so the array has four elements.
  2. Signed bytes. Java's byte ranges from -128 to 127, so the bit pattern 0xFF is shown as -1.
  3. 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);
    }
}

How It Works

  1. An exact copy. b.txt is made with Files.copy, so its bytes match a.txt.
  2. One letter differs. c.txt has a capital W, a different byte value.
  3. 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);
    }
}

How It Works

  1. 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.
  2. Hex formatting. %02X prints each unsigned byte as two upper-case hex digits; P, N, and G are 0x50, 0x4E, and 0x47.
  3. 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.

Next: Files.readAllLines()

Read a whole text file as a List of lines, without the line breaks.

readAllLines() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful