Files Mismatch Compare File Bytes

Compare two files byte by byte with Files.mismatch: identical content yields -1, otherwise you get the offset of the first divergent byte, no manual loops.

JavaData Integrity

Code

snippet
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class BuildArtifactCheck {

    public static void main(String[] args) throws IOException {
        Path left = Files.createTempFile("left-", ".txt");
        Path right = Files.createTempFile("right-", ".txt");
        Files.writeString(left, "sha256:aa11\nsize:1024\n", StandardCharsets.UTF_8);
        Files.writeString(right, "sha256:aa11\nsize:1024\n", StandardCharsets.UTF_8);

        // 逐字节比较(byte compare):完全相同返回 -1
        System.out.println("identical files: " + Files.mismatch(left, right));

        Files.writeString(right, "sha256:bb22\nsize:1024\n", StandardCharsets.UTF_8);
        // 内容不同时返回第一个不同字节的偏移(0 起)
        System.out.println("first diff at: " + Files.mismatch(left, right));

        Files.deleteIfExists(left);
        Files.deleteIfExists(right);
    }
}

How the snippet works

Files Mismatch Compare File Bytes keeps everything in the class BuildArtifactCheck.

Applying it

Running the file prints labeled results such as identical files:, first diff at: — each operation’s effect is visible in the output.

Statements that matter

Files.writeString(right, "sha256:aa11\nsize:1024\n", StandardCharsets.UTF_8);
Files.writeString(right, "sha256:bb22\nsize:1024\n", StandardCharsets.UTF_8);
Files.writeString(left, "sha256:aa11\nsize:1024\n", StandardCharsets.UTF_8);

Interactive Tools for This Topic

FAQ

What does Files Mismatch Compare File Bytes print when run?
Execute the whole file; each printed line is prefixed with a label like `identical files:`, `first diff at:`.