Java Files mismatch() Method: Compare Two Files Easily

Discover the mismatch() method in the Java Files class, introduced in Java 12, which simplifies file comparison. Learn how to efficiently identify differences between two files with this powerful method, enhancing your file-handling capabilities in Java applications.



Java - Files mismatch() Method

The mismatch() method is available in the Files class starting from Java 12. It provides an easy way to compare two files.

Syntax

Syntax

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

In this method:

  • If there is no mismatch, -1L is returned; otherwise, the position of the first mismatch is returned.
  • Mismatch occurs if file sizes do not match or if the byte contents are different.

Identifying Identical Files

A file is considered identical in the following scenarios:

  • If both locations point to the same file.
  • If the paths are the same and the file is not present, the files are considered the same.
  • If the files are of the same size and each byte in the first file matches the corresponding byte in the second file.

Parameters

  • path − The path to the first file.
  • path2 − The path to the second file.

Return Value

The method returns the position of the first mismatch or -1L if there is no mismatch.

Exceptions

  • IOException − Thrown if an I/O error occurs.
  • SecurityException − Thrown when a security manager is installed and checks read access to both files using the checkRead() method.

Files mismatch() Method Examples

Example: No Mismatch in Files

In this example, we create two text files in a temporary directory and write the same content to both files. Using the Files.mismatch() method, we check if the files are identical. Since the contents are the same, the output will indicate that the files matched.

Code Snippet

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

public class APITester {
public static void main(String[] args) throws IOException {
    // Create two files in the temp directory
    Path path1 = Files.createTempFile("file1", ".txt");
    Path path2 = Files.createTempFile("file2", ".txt");

    // Write same content to both files
    Files.writeString(path1, "exampleContent");
    Files.writeString(path2, "exampleContent");

    // Check for mismatch
    long mismatch = Files.mismatch(path1, path2);

    // Print message based on mismatch result
    if (mismatch > -1L) {
        System.out.println("Files matched");
    } else {
        System.out.println("Mismatch occurred in file1 and file2 at: " + mismatch);
    }
    
    // Delete the files
    path1.toFile().deleteOnExit();
    path2.toFile().deleteOnExit();
}
}
Output

Files matched

Example: Mismatch Identification in Files

In this example, we create two text files in a temporary directory and write different content to them. Using the Files.mismatch() method, we compare the files. Since the contents are different, the method returns the position of the first mismatch, which is printed as output.

Code Snippet

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

public class APITester {
public static void main(String[] args) throws IOException {
    // Create two files in the temp directory
    Path path1 = Files.createTempFile("file1", ".txt");
    Path path2 = Files.createTempFile("file2", ".txt");

    // Write different content to both files
    Files.writeString(path1, "exampleContent");
    Files.writeString(path2, "exampleContent with mismatch");

    // Check for mismatch
    long mismatch = Files.mismatch(path1, path2);

    // Print message based on mismatch result
    if (mismatch > -1L) {
        System.out.println("Mismatch occurred in file1 and file2 at: " + mismatch);
    } else {
        System.out.println("Files matched");
    }
    
    // Delete the files
    path1.toFile().deleteOnExit();
    path2.toFile().deleteOnExit();
}
}
Output

Mismatch occurred in file1 and file2 at: 16