Java 12 - Use Files.mismatch() to Compare Files

JDK 12 introduces a new method to the Files class. The method mismatch(Path, Path) compares the two specified files and returns the index of the first byte where they differ or -1 if they don’t.

  • static long mismatch(Path path, Path path2)​: Finds and returns the position of the first mismatched byte in the content of two files, or -1L if there is no mismatch. The position will be in the inclusive range of 0L up to the size (in bytes) of the smaller file.
FilesMismatch.java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class FilesMismatch {

    static Path createTempPath(String fileName) throws IOException {
        Path tempPath = Files.createTempFile(fileName, ".txt");
        tempPath.toFile().deleteOnExit();
        
        return tempPath;
    }
    
    public static void main(String[] args) throws IOException {
        Path filePath1 = createTempPath("test1");
        Path filePath2 = createTempPath("test2");
        Path filePath3 = createTempPath("test3");
        
        Files.writeString(filePath1, "Life is a progress, and not a station.");
        Files.writeString(filePath2, "Life is a progress, and not a station.");
        Files.writeString(filePath3, "Life is a progress, it's not a station.");

        long mismatch = Files.mismatch(filePath1, filePath2);
        System.out.println("File1 x File2 = " + mismatch); // match
        
        mismatch = Files.mismatch(filePath1, filePath3);
        System.out.println("File1 x File3 = " + mismatch); // mismatch
    }
}
                    

File1 x File2 = -1
File1 x File3 = 20

Two files are considered to match if they satisfy one of the following conditions:

  • The two paths locate the same file, even if two equal paths locate a file does not exist, or
  • The two files are the same size, and every byte in the first file is identical to the corresponding byte in the second file.

Otherwise there is a mismatch between the two files and the value returned by this method is:

  • The position of the first mismatched byte, or
  • The size of the smaller file (in bytes) when the files are different sizes and every byte of the smaller file is identical to the corresponding byte of the larger file.
Files RelationshipFiles.mismatch(Path,Path)
Same File-1 (match)
Copied File-1 (match)
Different Files, same content-1 (match)
Different Files, different content>0 (mismatch)
Soft-linked-1 (match)
Hard-linked-1 (match)

This method may not be atomic with respect to other file system operations. This method is always reflexive (for Path f, mismatch(f,f) returns -1L). If the file system and files remain static, then this method is symmetric (for two Paths f and g, mismatch(f,g) will return the same value as mismatch(g,f)).