Library contents

Java Files.readAllBytes and write

Move a small binary payload between a byte array and a file.

A simple example

Files.readAllBytes and write

  1. Start with Two bytes, 0 and 7
  2. Do this Write and read the bytes
  3. Result 0, 7
Binary I/O preserves the zero byte as part of the data.

Syntax

Java syntax
public static byte[] readAllBytes(Path path) throws IOException
JDK 21 declarations (4)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Store a three-level brightness preset

A photography student saves three brightness levels as bytes. The brightest level has all eight bits set, so the viewer must convert that stored byte before displaying its value on a 0–255 scale.

First output line3
Main.java
import java.nio.file.*;

class Main {
    public static void main(String[] args) throws Exception {
        Path desk = Files.createTempDirectory("file-lesson-");
        try {
            Path preset = desk.resolve("brightness.bin");
            Files.write(preset, new byte[]{0, 64, (byte) 255});
            byte[] levels = Files.readAllBytes(preset);
            System.out.println(levels.length);
            System.out.println(Byte.toUnsignedInt(levels[2]));
        } finally {
            try (var paths = Files.walk(desk)) {
                for (Path p : paths.sorted(java.util.Comparator.reverseOrder()).toList())
                    Files.deleteIfExists(p);
            }
        }
    }
}
Output
3
255

The file contains three bytes. Unsigned conversion recovers the intended value for the last level.

Advertisements

Append a second device sample

A device logs its measurements as individual bytes. When the second sample arrives, append it to the existing data so the first reading remains available for comparison.

Result[7, 9]
Main.java
import java.nio.file.*;
import java.util.*;

class Main {
    public static void main(String[] args) throws Exception {
        Path desk = Files.createTempDirectory("file-lesson-");
        try {
            Path log = Files.write(desk.resolve("samples.bin"), new byte[]{7});
            Files.write(log, new byte[]{9}, StandardOpenOption.APPEND);
            System.out.println(Arrays.toString(Files.readAllBytes(log)));
        } finally {
            try (var paths = Files.walk(desk)) {
                for (Path p : paths.sorted(java.util.Comparator.reverseOrder()).toList())
                    Files.deleteIfExists(p);
            }
        }
    }
}
Output
[7, 9]

The explicit append option adds one byte after the original measurement.

Read a display preset

A museum sign saves three brightness levels as individual bytes. Read the unsigned byte at the indicated zero-based offset. Which stored sample is returned?

Given12 · 80 · 200 | offset 2
Worked result200

What to watch for

readAllBytes needs enough memory for the full file. A Java byte is signed, so convert to unsigned when displaying values from 128 to 255.

Documentation for Files.readAllBytes and write