contentEquals()
is a FileUtils
class that is used to check whether the contents of two files are equal.
This method first checks to see if the two files are different lengths or if they point to the same file before resorting to the byte-by-byte comparison of the file contents.
FileUtils
The definition of FileUtils
can be found in the Apache Commons IO package, which we can add to the Maven project by adding the following dependency to the pom.xml
file:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
For other versions of the
commons-io
package, refer to the Maven Repository.
You can import the FileUtils
class as follows:
import org.apache.commons.io.FileUtils;
public static boolean contentEquals(final File file1, final File file2)
final File file1
: This is the first file.final File file2
: This is the second file.This method returns true
if the file contents are equal and returns false
otherwise.
import org.apache.commons.io.FileUtils;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;public class Main{private static void printFile(String filePath) throws IOException {BufferedReader bufferedReader= new BufferedReader(new FileReader(filePath));String string;while ((string = bufferedReader.readLine()) != null)System.out.println(string);}private static void wrapper(String path1, String path2) throws IOException {File file1 = new File(path1);File file2 = new File(path2);System.out.println("The contents of " + path1 + " is as follows:");printFile(path1);System.out.println("The contents of " + path2 + " is as follows:");printFile(path2);System.out.println("The output of FileUtils.contentEquals(file1, file2) - " + FileUtils.contentEquals(file1, file2));}public static void main(String[] args) throws IOException {// Example 1String filePath1 = "/Users/educative/Documents/temp/1.txt";String filePath2 = "/Users/educative/Documents/temp/2.txt";wrapper(filePath1, filePath2);System.out.println("-------");// Example 2wrapper(filePath1, filePath1);System.out.println("-------");// Example 3filePath2 = "/Users/educative/Documents/temp/3.txt";wrapper(filePath1, filePath2);}}
File 1 - 1.txt
File 2 - 2.txt
The method returns true
, indicating that the contents of both the files are equal.
File 1 - 1.txt
File 2 - 1.txt
The method returns true
, indicating that both of the files are the same.
File 1 - 1.txt
File 2 - 3.txt
The method returns false
, indicating that the contents of both the files are different.
In the above code, we define two private methods. One is to print the file given the path to the file. The other method is a wrapper method that takes two file paths, calls the printFile()
, and prints the output of the FileUtils.contentEquals()
method.
We use three different files, i.e., 1.txt
, 2.txt
, and 3.txt
. The contents of 1.txt
and 2.txt
are the same, but the content of 3.txt
is different from the other two.
The output of the code is as follows:
The contents of /Users/educative/Documents/temp/1.txt is as follows:
hello-educative
The contents of /Users/educative/Documents/temp/2.txt is as follows:
hello-educative
The output of FileUtils.contentEquals(file1, file2) - true
-------
The contents of /Users/educative/Documents/temp/1.txt is as follows:
hello-educative
The contents of /Users/educative/Documents/temp/1.txt is as follows:
hello-educative
The output of FileUtils.contentEquals(file1, file2) - true
-------
The contents of /Users/educative/Documents/temp/1.txt is as follows:
hello-educative
The contents of /Users/educative/Documents/temp/3.txt is as follows:
adfge
The output of FileUtils.contentEquals(file1, file2) - false