View Discussion Show Improve Article Save Article Like Article View Discussion Improve Article Save Article Like Article The path of two files can be compared lexicographically in Java using java.io.file.compareTo() method. It is useful to raise a Red Flag by the operating system when the program is requesting the file modification access which is already in use by another program. To compare the path of the file, compareTo() method of File Class is used. compareTo() method compares two abstract path-names lexicographically. The ordering defined by this method is dependent upon the operating system. Parameters: This method requires a single parameter i.e.the abstract pathname that is to be compared. Return Value: This method returns 0 if the argument is equal to this abstract pathname, a negative value if the abstract pathname is lexicographically less than the argument, and a value greater than 0 if the abstract pathname is lexicographically greater than the argument respectively. Example:
Output:
Write a java program to compare two text files line by line. Your program should take two text files as input and compare their content line by line and find out whether they have same content or not. How To Compare Two Text files Line By Line In Java?Step 1 : Define two BufferedReader objects, reader1 and reader2, to read the both input text files line by line. BufferedReader reader1 = new BufferedReader(new FileReader(“Pass the path of file1 here”)) BufferedReader reader2 = new BufferedReader(new FileReader(“Pass the path of file2 here”)) Step 2 : Initialize areEqual with true and lineNum with 1. areEqual will be a flag variable which will be initially set to true and it will be set to false when the content of input files differ. lineNum will hold the count of number of lines. boolean areEqual = true; int lineNum = 1; Step 3 : Read the lines of file1 into line1 and lines of file2 into line2. String line1 = reader1.readLine() String line2 = reader2.readLine() Step 4 : Keep reading the lines of file1 into line1 and lines of file2 into line2 till the end of the files. If any one of line1 or line2 is null, then assign false to areEqual and break the loop. If both, line1 and line2, are not null then compare them using equalsIgnoreCase() method. If it returns true then continue with the loop. Otherwise break the loop and assign false to areEqual. while (line1 != null || line2 != null) line1 = reader1.readLine(); Step 5 : If areEqual is true then declare both files have same content. If areEqual is false then declare both files have different content. Step 6 : Close the resources. Java Program To Compare Two Text Files Line By Line :import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class CompareTextFiles { public static void main(String[] args) throws IOException { BufferedReader reader1 = new BufferedReader(new FileReader("C:\\file1.txt")); BufferedReader reader2 = new BufferedReader(new FileReader("C:\\file2.txt")); String line1 = reader1.readLine(); String line2 = reader2.readLine(); boolean areEqual = true; int lineNum = 1; while (line1 != null || line2 != null) { if(line1 == null || line2 == null) { areEqual = false; break; } else if(! line1.equalsIgnoreCase(line2)) { areEqual = false; break; } line1 = reader1.readLine(); line2 = reader2.readLine(); lineNum++; } if(areEqual) { System.out.println("Two files have same content."); } else { System.out.println("Two files have different content. They differ at line "+lineNum); System.out.println("File1 has "+line1+" and File2 has "+line2+" at line "+lineNum); } reader1.close(); reader2.close(); } }Output 1 :file1 : Abhi 71 Bhavani 68 Mahesh 89 Nalini 62 Shloka 84 Kaya 84 Siya 56 Vikas 92
This post will discuss how to compare the contents of two files to determine whether they are equal in Java. There are several ways to compare the contents of two files to determine equality in plain Java and using third-party libraries. These are discussed below in detail: 1. Using Apache Commons IOApache Commons IO’s FileUtils class has several utility methods for working with files. To compare the contents of two files, we can use its contentEquals(…) method, which returns true only if the content of both files are equal or they both don’t exist. This method checks for the existence of both files, checks that both files are regular files and not a directory, compares the length of both files, or if they point to the same file, before resorting to the byte-by-byte comparison of the contents.
Download Code 2. Using Arrays.equals() methodIn JDK, we can simply read the entire files into byte arrays and then compare both arrays for equality. To read all the bytes from a file into a byte array, we can use Files.readAllBytes() method, and byte arrays equality can be checked using Arrays.equals(), as demonstrated below:
Download Code 3. Using BufferedInputStreamThe above approach is not recommended for large files as it might exhaust the heap memory. If the files are large, instead of reading the entire files into arrays, we should use BufferedReader and read the files chunk-by-chunk. ⮚ Read character-by-character using BufferedReader‘s read() method
Download Code ⮚ Read line-by-line using BufferedReader‘s readLine() method
Download Code That’s all about comparing the contents of two files for determining equality in Java.
Thanks for reading. Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages. Like us? Refer us to your friends and help us grow. Happy coding 🙂 |