1. Java 12 Files mismatch Overview
In this post, We will learn about new method mismatch() added in Java 12 to Files class. Files class is in package java.nio.file.Files.java.nio.file.Files class consists exclusively of static methods that operate on files, directories, or other types of files. In most cases, the methods defined here will delegate to the associated file system provider to perform the file operations.
We will learn how to compare two files using Files.mismatch method.
JDK 12 introduces the new way to determine equality between two files.
2. Files mismatch() Method
2.1 Description
Finds and returns the position of the first mismatched byte in the content of two files, or -1L if there is no mismatch. The position will be in the inclusive range of 0L up to the size (in bytes) of the smaller file.
2.2 Syntax
public static long mismatch(Path path, Path path2) throws IOException
2.3 Parameters
path - the path to the first file path2 - the path to the second file
2.4 Returns
The position of the first mismatch or -1L if no mismatch2.5 Conditions
Two files are considered to match if they satisfy one of the following conditions:
The two paths locate the same file, even if two equal paths locate a file does not exist, or
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:
The position of the first mismatched byte, or
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.
2.6 Exceptions to be thrown
IOException - if an I/O error occurs SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to both files.
3. Files mismatch() Method Example
This method is used to compare two files content. We will test this method with different scenarios as below.
3.1 Files with same content
Here taking two files file1.txt and file2.txt. Taken content in both files is "first line".
file1.txt content:
first line
file2.txt content:
first line
mismatch method takes two files path and compares the content char by char. If same returns -l otherwise returns first position of mismatch. See the below example how to use mismatch method.
package examples.java.w3schools.files; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class MismatchExample { public static void main(String[] args) { Path path1 = Paths .get("C:\\Demo-Java-12\\file1.txt"); Path path2 = Paths .get("C:\\Demo-Java-12\\file2.txt"); try { long diff = Files.mismatch(path1, path2); if (diff == -1L) { System.out.println("file1.txt and file2.txt files are having identical content"); } else { System.out.println("file1.txt and file2.txt are non identical"); } } catch (IOException e) { e.printStackTrace(); } } }
Output:
The above program compiles and run successfully in Java 12 version and produces this output.
file1.txt and file2.txt files are having identical content
3.2 Files with Non-Identical Content
We will take two temp files which are having different content as in below example.
package examples.java.w3schools.java12.files; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; public class MismatchExampleSameContent { public static void main(String[] args) { try { Path filePath1 = Files.createTempFile("my-file", ".txt"); Path filePath2 = Files.createTempFile("my-file2", ".txt"); Files.writeString(filePath1, "This is a text file 1"); Files.writeString(filePath2, "This is a text file 2 for testing"); long diff = Files.mismatch(filePath1, filePath2); System.out.println("file1.txt and file2.txt are non identical"); System.out.println("Found different character at index: " + diff); } catch (IOException e) { e.printStackTrace(); } } }
Output:
file1.txt and file2.txt are non identical Found different character at index: 20
3.3 Passing same file names to path1 and path2
We will be passing the files as below. path1 is passed to First and second parameter.package examples.java.w3schools.java12.files; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class MismatchExampleSameFile { public static void main(String[] args) { Path path1 = Paths .get("C:\\Demo-Java-12\\file1.txt"); Path path2 = Paths .get("C:\\Demo-Java-12\\file2.txt"); try { long diff = Files.mismatch(path1, path1); System.out.println("file1.txt and file2.txt files are having identical content"); System.out.println("Files are same and no diff character found because returned " + diff); } catch (IOException e) { e.printStackTrace(); } } }
Output:
file1.txt and file2.txt files are having identical content Files are same and no diff character found because returned -1
JDK 12 Early Access Build 20
4. Conclusion
In this article, We've seen what is new method added in java 12 Files API. How to compare two files content using Files mismatch() method.
Next, written exmaple programs on if two files are having identical and non indentical contents.
Finally, Implemented if two file location are same then what output will be produces.
All the program shown in this article are on GitHub.
No comments:
Post a Comment
Please do not add any spam links in the comments section.