Compare contents of two files to determine equality in Java
This post will discuss how to compare the contents of two files to determine whether they are equal in Java.
There are several ways to compare the contents of two files to determine equality in plain Java and using third-party libraries. These are discussed below in detail:
1. Using Apache Commons IO
Apache Commons IO’s FileUtils class has several utility methods for working with files. To compare the contents of two files, we can use its contentEquals(…) method, which returns true only if the content of both files are equal or they both don’t exist.
This method checks for the existence of both files, checks that both files are regular files and not a directory, compares the length of both files, or if they point to the same file, before resorting to the byte-by-byte comparison of the contents.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; class Main { private static boolean isEqual(File firstFile, File secondFile) { try { return FileUtils.contentEquals(firstFile, secondFile); } catch (IOException e) { e.printStackTrace(); return false; } } public static void main(String[] args) { File firstFile = new File("/var/www/first.txt"); File secondFile = new File("/var/www/second.txt"); boolean equal = isEqual(firstFile, secondFile); if (equal) { System.out.println("Files are equal."); } else { System.out.println("Files are not equal."); } } } |
2. Using Arrays.equals() method
In JDK, we can simply read the entire files into byte arrays and then compare both arrays for equality. To read all the bytes from a file into a byte array, we can use Files.readAllBytes() method, and byte arrays equality can be checked using Arrays.equals(), as demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; class Main { private static boolean isEqual(Path firstFile, Path secondFile) { try { if (Files.size(firstFile) != Files.size(secondFile)) { return false; } byte[] first = Files.readAllBytes(firstFile); byte[] second = Files.readAllBytes(secondFile); return Arrays.equals(first, second); } catch (IOException e) { e.printStackTrace(); } return false; } public static void main(String[] args) { File firstFile = new File("/var/www/first.txt"); File secondFile = new File("/var/www/second.txt"); boolean equal = isEqual(firstFile.toPath(), secondFile.toPath()); if (equal) { System.out.println("Files are equal."); } else { System.out.println("Files are not equal."); } } } |
3. Using BufferedInputStream
The above approach is not recommended for large files as it might exhaust the heap memory. If the files are large, instead of reading the entire files into arrays, we should use BufferedReader and read the files chunk-by-chunk.
⮚ Read character-by-character using BufferedReader‘s read() method
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; class Main { private static boolean isEqual(Path firstFile, Path secondFile) { try { long size = Files.size(firstFile); if (size != Files.size(secondFile)) { return false; } if (size < 2048) { return Arrays.equals(Files.readAllBytes(firstFile), Files.readAllBytes(secondFile)); } // Compare character-by-character try (BufferedReader bf1 = Files.newBufferedReader(firstFile); BufferedReader bf2 = Files.newBufferedReader(secondFile)) { int ch; while ((ch = bf1.read()) != -1) { if (ch != bf2.read()) { return false; } } } return true; } catch (IOException e) { e.printStackTrace(); } return false; } public static void main(String[] args) { File firstFile = new File("/var/www/first.txt"); File secondFile = new File("/var/www/second.txt"); boolean equal = isEqual(firstFile.toPath(), secondFile.toPath()); if (equal) { System.out.println("Files are equal."); } else { System.out.println("Files are not equal."); } } } |
⮚ Read line-by-line using BufferedReader‘s readLine() method
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; class Main { private static boolean isEqual(Path firstFile, Path secondFile) { try { long size = Files.size(firstFile); if (size != Files.size(secondFile)) { return false; } if (size < 2048) { return Arrays.equals(Files.readAllBytes(firstFile), Files.readAllBytes(secondFile)); } // Compare line-by-line try (BufferedReader bf1 = Files.newBufferedReader(firstFile); BufferedReader bf2 = Files.newBufferedReader(secondFile)) { String line; while ((line = bf1.readLine()) != null) { if (line != bf2.readLine()) { return false; } } } return true; } catch (IOException e) { e.printStackTrace(); } return false; } public static void main(String[] args) { File firstFile = new File("/var/www/first.txt"); File secondFile = new File("/var/www/second.txt"); boolean equal = isEqual(firstFile.toPath(), secondFile.toPath()); if (equal) { System.out.println("Files are equal."); } else { System.out.println("Files are not equal."); } } } |
That’s all about comparing the contents of two files for determining equality in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)