SoFunction
Updated on 2025-03-09

Java SE multiple method codes for determining whether the contents of two files are the same

1. Byte byte comparison

Compare file contents by byte. This method is suitable for small files, but it will be time-consuming for large files.

import ;
import ;
import ;

public boolean areFilesEqual(Path file1, Path file2) throws IOException {
    return (file1, file2) == -1;
}
import ;
import ;
import ;
import ;

public boolean areFileContentsEqual(Path file1, Path file2) throws IOException {
    byte[] content1 = (file1);
    byte[] content2 = (file2);
    return (content1, content2);
}
import ;
import ;
import ;
import ;

public static boolean areFileContentsEqual(Path file1, Path file2) throws IOException {
    try (InputStream is1 = (file1);
         InputStream is2 = (file2)) {
        int byte1, byte2;
        
        do {
            byte1 = ();
            byte2 = ();
            if (byte1 != byte2) {
                return false;
            }
        } while (byte1 != -1);
        
        return true;
    }
}

2. File summary (hash value) comparison

Calculate the hash value of the file (such as MD5, SHA-256, etc.), and then compare the hash value of the two files. If the hash value is the same, the file content can be considered the same. This approach works for large files because only hash values ​​are needed to compare the contents of the entire file.

import ;
import ;
import ;
import ;
import ;

public boolean areFilesEqual(byte[] input1, byte[] input2) throws IOException, NoSuchAlgorithmException {
    MessageDigest md5 = ("MD5");

    byte[] file1Hash = (input1);
    byte[] file2Hash = (input2);

    return (file1Hash, file2Hash);
}

3. FileChannel

Use FileChannel to read the file contents block by block, and then compare the read contents block by block. This approach avoids array copying every time the data is read.

public boolean areFileContentsEqual(Path file1, Path file2) throws IOException {
    try (FileChannel channel1 = (file1, );
         FileChannel channel2 = (file2, )) {

        long size1 = ();
        long size2 = ();

        if (size1 != size2) {
            // File sizes are different, contents cannot be equal
            return false;
        }

        ByteBuffer buffer1 = (8192);
        ByteBuffer buffer2 = (8192);

        while ((buffer1) != -1) {
            ();
            (buffer2);
            ();

            if (!(buffer2)) {
                // File contents are not equal
                return false;
            }

            ();
            ();
        }

        return true;
    }
}

In the above code, we open the FileChannel of the two files, and then read the contents of the two files block by block by block by specified buffer size (e.g. 8192 bytes) and compare. If any piece of content is not equal, return false immediately to indicate that the file content is different. If the contents of the entire file are compared and no differences are found, return true to indicate the contents of the file.

Note that this approach works for large files, as it avoids loading the entire file into memory at once, but rather compares the file contents one by one by one. Depending on specific requirements and performance requirements, you can adjust the buffer size to optimize comparison speed.

4. File metadata comparison

Compare the metadata of the file, including file name, file size, modification time, etc. This method is quick and easy and suitable for scenarios where it is necessary to quickly determine whether the file is the same.

import ;
import ;
import ;
import ;

public boolean areFilesEqual(Path file1, Path file2) throws IOException {
    BasicFileAttributes attrs1 = (file1, );
    BasicFileAttributes attrs2 = (file2, );

    return () == () &&
           ().equals(());
}

5. Apache Commons IO Library

Use the contentEquals() method provided by the FileUtils class in the Apache Commons IO library to compare whether the contents of two files are the same.

import ;

public boolean areFilesEqual(File file1, File file2) throws IOException {
    return (file1, file2);
}

6. Hutool Library

import ;

public boolean areFilesEqual(File file1, File file2) throws IOException {
    (file1,file2)
}

Summarize

This is the end of this article about multiple methods for judging whether the contents of two files are the same by Java SE. For more related Java SE to determine the contents of the same file, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!