Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is FileUtils.contentEqualsIgnoreEOL() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

contentEqualsIgnoreEOL() is a staticMethods in Java that can be called without creating an object of the class. method of the 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.

How to import 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;

Syntax

public static boolean contentEqualsIgnoreEOL(final File file1, final File file2, final String charsetName)

Parameters

  • 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.

Return value

This method returns true if the file contents are equal and false otherwise.

Code

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 1
String filePath1 = "/Users/educative/Documents/temp/1.txt";
String filePath2 = "/Users/educative/Documents/temp/2.txt";
wrapper(filePath1, filePath2);
System.out.println("-------");
// Example 2
wrapper(filePath1, filePath1);
System.out.println("-------");
// Example 3
filePath2 = "/Users/educative/Documents/temp/3.txt";
wrapper(filePath1, filePath2);
}
}

Explanation

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.

Example 1

  • File 1: 1.txt
  • File 2: 2.txt

The method returns true, indicating that the contents of both the files are equal.

Example 2

  • File 1: 1.txt
  • File 2: 1.txt

The method returns true, indicating that both the files are the same.

Example 3

  • File 1: 1.txt
  • File 2: 3.txt

The method returns false, indicating that the contents of both the files are different.

Output

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

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring