Learnitweb

Comparing file with Files.mismatch() method in Java

1. Introduction

In Java 12, mismatch() method was added to compare two files. This method returns the position of the first mismatched byte in the two files. Two files are considered a match if one of the two following conditions are satisfied:

  • The two paths refer to the same file location. It is possible that both paths locate a file that does not exist.
  • Two files are the same size and a match byte by byte.

If none of the conditions are satisfied then the files are considered a mismatch. If the files are a mismatch, then the method returns:

  • The position of the first mismatched byte, or
  • The size (in bytes) of the smaller file, when the files have different sizes and every byte in the smaller file matches the corresponding byte in the larger file.

2. Syntax

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

Parameters

  • path – the path to the first file
  • path2 – the path to the second file

Returns

Identifies and provides the position of the first mismatched between the contents of two files, or -1L if no differences exist. So this method can return the 0L to the size (in bytes) of the smaller file, inclusive.

3. Examples

3.1 Comparing two different files

Suppose following are the contents of two files, firstFile.txt and secondFile.txt.

//Content of first file
This is my first message: Hello World!

//Content of second file
This is my first message- Hello World!

Here, is the code.

public class FileMismatchExample {
    public static void main(String[] args) throws IOException {

        Path filePath1 = Path.of("D:\\firstFile.txt");
        Path filePath2 = Path.of("D:\\secondFile.txt");

        // calling the mismatchfunction
        long mismatch = Files.mismatch(filePath1, filePath2);
        
        if (mismatch == -1)
            System.out.println("No mismatch found in the files");
        else
            System.out.println("mismatch found at:" + mismatch);
    }
}

Output

mismatch found at:24

3.2 Matching same files

Here, we are comparing the same file.

public class FileMismatchExample {
    public static void main(String[] args) throws IOException {

        Path filePath1 = Path.of("D:\\firstFile.txt");
        Path filePath2 = Path.of("D:\\firstFile.txt");

        // calling the mismatchfunction
        long mismatch = Files.mismatch(filePath1, filePath2);

        if (mismatch == -1)
            System.out.println("No mismatch found in the files");
        else
            System.out.println("mismatch found at:" + mismatch);
    }
}

Output

No mismatch found in the files

4. Conclusion

In this tutorial, we explored the Files.mismatch() method in Java for comparing files. This powerful method allows for efficient identification of the first differing byte between two files, or confirms their complete match. By leveraging Files.mismatch(), you can simplify file comparison operations, making your code cleaner and more efficient. Whether dealing with large datasets, performing file integrity checks, or ensuring data consistency, the Files.mismatch() method is a valuable tool in a Java developer’s toolkit. Remember to handle exceptions appropriately and consider edge cases such as empty files or different file sizes to ensure robust and reliable file comparison in your applications.