contentEqualsIgnoreEOL()
is a FileUtils
class that is used to compare the contents of two files to determine if they are equal.
This method checks to see if the two files point to the same file before resorting to a line-by-line comparison of the 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 contentEqualsIgnoreEOL(final File file1, final File file2, final String charsetName)
final File file1
: This is the first file.final File file2
: This is the second file.final String charsetName
: This is the name of the requested charset. If null
, the platform default is used.This method returns true
if the file contents are equal and false
otherwise.
import org.apache.commons.io.FileUtils;import java.io.*;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.contentEqualsIgnoreEOL(file1, file2, null));}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);}}
In the code above, we define two private methods. One method prints the file given the path to the file, and the other method is a wrapper method that takes two file paths, calls the printFile()
, and prints the output of the FileUtils.contentEqualsIgnoreEOL()
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.
1.txt
2.txt
The method returns true
, indicating that the contents of both the files are equal.
1.txt
1.txt
The method returns true
, indicating that both the files are the same.
1.txt
3.txt
The method returns false
, indicating that the contents of both the files are different.
The output of the code will be as follows:
The contents of /Users/educative/Documents/temp/1.txt is as follows:
hello-educative
hi-edpresso
The contents of /Users/educative/Documents/temp/2.txt is as follows:
hello-educative
hi-edpresso
The output of FileUtils.contentEquals(file1, file2) - true
-------
The contents of /Users/educative/Documents/temp/1.txt is as follows:
hello-educative
hi-edpresso
The contents of /Users/educative/Documents/temp/1.txt is as follows:
hello-educative
hi-edpresso
The output of FileUtils.contentEquals(file1, file2) - true
-------
The contents of /Users/educative/Documents/temp/1.txt is as follows:
hello-educative
hi-edpresso
The contents of /Users/educative/Documents/temp/3.txt is as follows:
sfdg
bakcside
The output of FileUtils.contentEquals(file1, file2) - false
Free Resources