Compare Contents of two Reader in Java using Apache Commons IO

Tags: Compare IOUtils Apache Commons Apache Commons IO Reader FileReader

In this Java tutorial we show you how to compare the contents of two Reader in Java using the IOUtils class of Apache Commons IO library.

How to add Apache Commons IO library to your Java project

To use the Apache Commons IO library in the Gradle build project, add the following dependency into the build.gradle file.

implementation 'commons-io:commons-io:2.8.0'

To use the Apache Commons IO library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.8.0</version>
</dependency>

To have more information about the Apache Commons IO library you can visit the library home page at commons.apache.org/proper/commons-io/

How to compare the contents of two Reader

In the following Java program, we show you how to use the IOUtils.contentEquals() of Apache Commons IO library to compare two Reader objects.

CompareTwoReader.java

import org.apache.commons.io.IOUtils;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class CompareTwoReader {
    public static void main(String... args) {
        try {
            Reader reader1 = new FileReader("D:\\Data\\data.txt");
            Reader reader2 = new FileReader("D:\\Data\\data.txt");
            Reader reader3 = new FileReader("D:\\Data\\data1.txt");

            boolean result1 = IOUtils.contentEquals(reader1, reader2);
            boolean result2 = IOUtils.contentEquals(reader1, reader3);

            System.out.println("Result of compare data.txt and data.txt: " + result1);
            System.out.println("Result of compare data.txt and data1.txt: " + result2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
The output is:
Result of compare data.txt and data.txt: true
Result of compare data.txt and data1.txt: false

In the following Java program, we show you how to use the IOUtils.contentEquals() of Apache Commons IO library to compare two Reader objects and ignore the EOL characters.

CompareTwoReader2.java

import org.apache.commons.io.IOUtils;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class CompareTwoReader2 {
    public static void main(String... args) {
        try {
            Reader reader1 = new FileReader("D:\\Data\\data.txt");
            Reader reader2 = new FileReader("D:\\Data\\data.txt");
            Reader reader3 = new FileReader("D:\\Data\\data1.txt");

            boolean result1 = IOUtils.contentEqualsIgnoreEOL(reader1, reader2);
            boolean result2 = IOUtils.contentEqualsIgnoreEOL(reader1, reader3);

            System.out.println("Result of compare data.txt and data.txt: " + result1);
            System.out.println("Result of compare data.txt and data1.txt: " + result2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
The output is:
Result of compare data.txt and data.txt: true
Result of compare data.txt and data1.txt: false

Happy Coding 😊