Java 12 brought some useful additions and comparing two files using a newly added method ‘Mismatch()’ is one of them. In this post, we will learn “How to use Files.mismatch() method introduced in Java 12?”. 

Here, we are going to learn the following things:

  1. What is Files.mismatch() method?
  2. How to use it?

Compare files with Files.mismatch() in Java 12...!!! Share on X

Let’s Begin,

1. What is Files.mismatch() method?

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

Files_Mismatch_Java12_Signature

2. How to use it?

Let’s take an example where we have a few temp files. First, we are comparing the files which are identical, and later comparing the unidentical ones using ‘mismatch()’ method. 

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

public class File_Mismatch_Java12_Example {

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

        long mismatch;

        System.out.println("Example of MATCHED Files\n");

        //Create 2 temp files (File1 and File2)
        Path file_Path1 = Files.createTempFile("File1", ".txt");
        Path file_Path2 = Files.createTempFile("File2", ".txt");

        //Write same content to both temp files
        Files.writeString(file_Path1, "Techndeck - For Programmers & Entrepreneurs");
        Files.writeString(file_Path2, "Techndeck - For Programmers & Entrepreneurs");

        //Compare the files
        mismatch = Files.mismatch(file_Path1, file_Path2);

        System.out.println("RULE - If files match, returned value is -1");

        if (mismatch == -1) {
            System.out.println("Result -> Files Matched where Mismatch Position in  File 1 & File 2 is: " + mismatch);
        } else {
            System.out.println("Result -> Files did not Match where Mismatch Position in  File 1 & File 2 is: " + mismatch);
        }

        //Delete both the files
        file_Path1.toFile().deleteOnExit();
        file_Path2.toFile().deleteOnExit();

        System.out.println("\nExample of UNMATCHED Files\n");

        //Example with  unmatched files

        //Create 2 temp files (File1 and File2)
        Path file_Path3 = Files.createTempFile("File3", ".txt");
        Path file_Path4 = Files.createTempFile("File4", ".txt");

        //Write different content to both temp files
        Files.writeString(file_Path3, "Techndeck");
        Files.writeString(file_Path4, "Deepak Verma");

        //Compare the files
        mismatch = Files.mismatch(file_Path3, file_Path4);

        System.out.println("RULE - If files match, returned value is -1");

        if (mismatch == -1) {
            System.out.println("Result -> Files Matched where Mismatch Position in  File 1 & File 2 is: " + mismatch);
        } else {
            System.out.println("Result -> Files did not Match where Mismatch Position in  File 1 & File 2 is: " + mismatch);
        }

        //Delete both the files
        file_Path3.toFile().deleteOnExit();
        file_Path4.toFile().deleteOnExit();

    }

}

Output:

Example of MATCHED Files

RULE - If files match, returned value is -1
Result -> Files Matched where Mismatch Position in  File 1 & File 2 is: -1

Example of UNMATCHED Files

RULE - If files match, returned value is -1
Result -> Files did not Match where Mismatch Position in  File 1 & File 2 is: 0

Compare files with Files.mismatch() in Java 12...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts