Library contents

Java Files.isSameFile and mismatch

Distinguish identity from content equality and locate the first differing byte.

A simple example

Files.isSameFile and mismatch

  1. Start with One file contains A, another B
  2. Do this Compare their contents
  3. Result Different
They have the same length but their first bytes differ.

Syntax

Java syntax
public static boolean isSameFile(Path path, Path path2) throws IOException
JDK 21 declarations (2)
Run these examples in Java

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

java Main.java

Verify a backup before and after an edit

A librarian copies a shelf list to a backup file. First verify whether the copy has the same contents and identity as the source, then locate the character changed in a later revision.

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

class Main {
    public static void main(String[] args) throws Exception {
        Path desk = Files.createTempDirectory("file-lesson-");
        try {
            Path source = Files.writeString(desk.resolve("shelf.txt"), "A17");
            Path backup = Files.copy(source, desk.resolve("backup.txt"));
            System.out.println(Files.isSameFile(source, backup));
            System.out.println(Files.mismatch(source, backup));
            Files.writeString(backup, "A18");
            System.out.println(Files.mismatch(source, backup));
        } finally {
            try (var paths = Files.walk(desk)) {
                for (Path p : paths.sorted(java.util.Comparator.reverseOrder()).toList())
                    Files.deleteIfExists(p);
            }
        }
    }
}
Output
false
-1
2

The third ASCII character differs, so its zero-based byte offset is two.

Advertisements

Detect a backup cut short in transit

A device transfers only the beginning of a receipt. Compare the original and the shortened copy to find the first missing position even though every byte present in both files agrees.

Result2
Main.java
import java.nio.file.*;

class Main {
    public static void main(String[] args) throws Exception {
        Path desk = Files.createTempDirectory("file-lesson-");
        try {
            Path original = Files.writeString(desk.resolve("original"), "A17");
            Path shortCopy = Files.writeString(desk.resolve("copy"), "A1");
            System.out.println(Files.mismatch(original, shortCopy));
        } finally {
            try (var paths = Files.walk(desk)) {
                for (Path p : paths.sorted(java.util.Comparator.reverseOrder()).toList())
                    Files.deleteIfExists(p);
            }
        }
    }
}
Output
2

When one file is a proper prefix, the mismatch offset is the length of the shorter file.

Prepare a gallery label

A curator stores a short UTF-8 caption beside an exhibit. Compare the note with a byte-for-byte backup immediately after copying it. What is their content relationship?

GivenFile contents “clay”
Worked resultIdentical

What to watch for

A mismatch of minus one means the byte sequences match. Two independent files can contain equal bytes.

Documentation for Files.isSameFile and mismatch