logo

Apache Commons IO to Compare File Content of InputStream or Reader using IOUtils

Java Code Examples for org.apache.commons.io.IOUtils.contentEquals() and org.apache.commons.io.IOUtils.contentEqualsIgnoreEOL()

Three examples below to show how to use Apache Commons IO IOUtils class to Compare File Content of InputStream objects or Reader objects.

Example IOUtils.contentEquals() method to compare 2 InputStream objects

package simplesolution.dev;

import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

public class IOUtilsContentEqualsInputStreamExample {

    public static void main(String... args) {
        try {
            File file1 = new File("D:\\Data\\file1.txt");
            File file2 = new File("D:\\Data\\file2.txt");
            Path path1 = file1.toPath();
            Path path2 = file2.toPath();
            InputStream inputStream1 = Files.newInputStream(path1);
            InputStream inputStream2 = Files.newInputStream(path2);

            boolean result = IOUtils.contentEquals(inputStream1, inputStream2);

            if(result) {
                System.out.println("File " + file1.getPath() + " and " + file2.getPath() + " are equal.");
            } else {
                System.out.println("File " + file1.getPath() + " and " + file2.getPath() + " are not equal.");
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Example IOUtils.contentEquals() method to compare 2 Reader objects

package simplesolution.dev;

import org.apache.commons.io.IOUtils;

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

public class IOUtilsContentEqualsReaderExample {

    public static void main(String... args) {
        try {
            Reader reader1 = new FileReader("D:\\Data\\file1.txt");
            Reader reader2 = new FileReader("D:\\Data\\file2.txt");

            boolean result = IOUtils.contentEquals(reader1, reader2);

            if(result) {
                System.out.println("2 files are equal.");
            } else {
                System.out.println("2 files are not equal.");
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Example IOUtils.contentEqualsIgnoreEOL() method to compare 2 Reader objects

package simplesolution.dev;

import org.apache.commons.io.IOUtils;

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

public class IOUtilsContentEqualsIgnoreEOLReaderExample {

    public static void main(String... args) {
        try {
            Reader reader1 = new FileReader("D:\\Data\\file1.txt");
            Reader reader2 = new FileReader("D:\\Data\\file2.txt");

            boolean result = IOUtils.contentEqualsIgnoreEOL(reader1, reader2);

            if(result) {
                System.out.println("2 files are equal.");
            } else {
                System.out.println("2 files are not equal.");
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Happy Coding 😊