Java 12 Tutorial
Selected Reading
- Java 12 - Discussion
- Java 12 - Useful Resources
- Java 12 - Quick Guide
- Java 11 Tutorial
- Java 10 Tutorial
- Java 9 Tutorial
- Java 8 Tutorial
- Java Tutorial
- Java 12 - Microbenchmark
- Garbage Collection Enhancements
- Java 12 - String methods
- Java 12 - Teeing Collectors
- Java 12 - Compact Number Formatting
- Java 12 - File mismatch method
- Java 12 - Switch Expressions
- Java 12 - Environment Setup
- Java 12 - Overview
- Java 12 - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java 12 - File mismatch method
Java 12 - File mismatch method
Java 12 introduces an easy way to compare two files using following syntax −
pubpc static long mismatch(Path path1, Path path2) throws IOException
Where
If there is no mismatch then 1L is returned else position of first mismatch is returned.
Mismatch is accounted in case if file sizes are not matching or byte contents are not matching.
Consider the following example −
ApiTester.java
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; pubpc class APITester { pubpc static void main(String[] args) throws IOException { Path path1 = Files.createTempFile("file1", ".txt"); Path path2 = Files.createTempFile("file2", ".txt"); Files.writeString(path1, "tutorialspoint"); Files.writeString(path2, "tutorialspoint"); long mismatch = Files.mismatch(path1, path2); if(mismatch > 1L) { System.out.println("Mismatch occurred in file1 and file2 at : " + mismatch); }else { System.out.println("Files matched"); } System.out.println(); Path path3 = Files.createTempFile("file3", ".txt"); Files.writeString(path3, "tutorialspoint Java 12"); mismatch = Files.mismatch(path1, path3); if(mismatch > 1L) { System.out.println("Mismatch occurred in file1 and file3 at : " + mismatch); }else { System.out.println("Files matched"); } path1.toFile().deleteOnExit(); path2.toFile().deleteOnExit(); path3.toFile().deleteOnExit(); } }
Output
Files matched Mismatch occurred in file1 and file3 at : 14Advertisements