Saturday 30 January 2021

Java12: Find mismatch in contents of two files

'java.nio.file.Files' class provide 'mismatch' method, it takes two files as input and finds and returns the position of the first mismatched byte in the content of two files, or -1L if there is no mismatch.

 

Signature

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:

the position of the first mismatch or -1L if no mismatch

 

When can two files are considered to be same?

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

a.   The two paths locate the same file, even if two equal paths locate a file does not exist, or

b.   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:

 

a.   The position of the first mismatched byte, or

b.   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.

 

Find the below working application.

 

FileMismatchExample.java

package com.sample.app.files;

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

public class FileMismatchExample {

	private static void writeToFile(Path path, String content) throws IOException {
		Files.write(path, content.getBytes());
	}

	public static void main(String args[]) throws IOException {
		File file1 = File.createTempFile("file1", ".txt");
		File file2 = File.createTempFile("file2", ".txt");
		File file3 = File.createTempFile("file3", ".txt");

		String str1 = "Hello World";
		String str2 = "Helloworld";

		Path path1 = file1.toPath();
		Path path2 = file2.toPath();
		Path path3 = file3.toPath();

		writeToFile(path1, str1);
		writeToFile(path2, str1);
		writeToFile(path3, str2);

		long offset = Files.mismatch(path1, path2);
		if (offset == -1l) {
			System.out.println(path1 + " and " + path2 + " has same content");
		} else {
			System.out.println(path1 + " and " + path2 + " do not have same content. Mismatch found at byte " + offset);
		}

		offset = Files.mismatch(path1, path3);
		if (offset == -1l) {
			System.out.println(path1 + " and " + path3 + " has same content");
		} else {
			System.out.println(path1 + " and " + path3 + " do not have same content. Mismatch found at byte " + offset);
		}

	}

}

Sample output

/var/folders/tp/qybw2qy54t39ffn2l0grsdrc0000gp/T/file13438498583646993710.txt and /var/folders/tp/qybw2qy54t39ffn2l0grsdrc0000gp/T/file214420760322000149874.txt has same content
/var/folders/tp/qybw2qy54t39ffn2l0grsdrc0000gp/T/file13438498583646993710.txt and /var/folders/tp/qybw2qy54t39ffn2l0grsdrc0000gp/T/file313488649827910837929.txt do not have same content. Mismatch found at byte 5

Reference

https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/nio/file/Files.html#mismatch(java.nio.file.Path,java.nio.file.Path)

 




Previous                                                    Next                                                    Home

No comments:

Post a Comment