Close

Java 12 - Comparing files with Files.mismatch()

[Last Updated: Aug 11, 2020]

Java 12 added following method in java.nio.file.Files class to compare two files:

public static long mismatch(Path path, Path path2) throws IOException

This method returns the position of the first mismatch or -1L if there's no mismatch.

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

  • The two files have the same size, and every byte in the first file is identical to the corresponding byte in the second file.
  • The two paths represent the same file, even if the file does not exist.

In case of mismatch files, the return value is one of the the followings:

  • The position of the first mismatched byte
  • The size of the smaller file (in bytes) when the files are of different sizes and every byte of the smaller file is identical to the corresponding byte of the larger file.

Examples

Files have identical content

package com.logicbig.example;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class MismatchExample1 {
  public static void main(String[] args) throws IOException {
      Path filePath1 = Files.createTempFile("my-file", ".txt");
      Path filePath2 = Files.createTempFile("my-file2", ".txt");
      Files.writeString(filePath1,"a test string");
      Files.writeString(filePath2,"a test string");

      long mismatch = Files.mismatch(filePath1, filePath2);
      System.out.println(mismatch);

      filePath1.toFile().deleteOnExit();
      filePath2.toFile().deleteOnExit();
  }
}
-1

Files have non-identical content

package com.logicbig.example;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class MismatchExample2 {
  public static void main(String[] args) throws IOException {
      Path filePath1 = Files.createTempFile("my-file", ".txt");
      Path filePath2 = Files.createTempFile("my-file2", ".txt");
      Files.writeString(filePath1,"a test string");
      Files.writeString(filePath2,"a test string ....");

      long mismatch = Files.mismatch(filePath1, filePath2);
      System.out.println(mismatch);

      filePath1.toFile().deleteOnExit();
      filePath2.toFile().deleteOnExit();
  }
}
13

Same paths

package com.logicbig.example;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class MismatchExample3 {
  public static void main(String[] args) throws IOException {
      Path filePath1 = Path.of("c:\\temp\\testFile.txt");
      Path filePath2 = Path.of("c:\\temp\\testFile.txt");
      System.out.println("file exists: "+Files.exists(filePath1));
      long mismatch = Files.mismatch(filePath1, filePath2);
      System.out.println(mismatch);

      filePath1.toFile().deleteOnExit();
      filePath2.toFile().deleteOnExit();
  }
}
file exists: false
-1

Example Project

Dependencies and Technologies Used:

  • JDK 12
Files.mismatch() Example Select All Download
  • java-12-files-mismatch-method
    • src
      • com
        • logicbig
          • example
            • MismatchExample1.java

    See Also