【Java SE】判断两个文件内容是否相同的多种方法

1. 逐字节比较

逐字节比较文件内容。这种方法适用于小文件,但对于大文件会比较耗时。

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

public boolean areFilesEqual(Path file1, Path file2) throws IOException {
    return Files.mismatch(file1, file2) == -1;
}
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;

public boolean areFileContentsEqual(Path file1, Path file2) throws IOException {
    byte[] content1 = Files.readAllBytes(file1);
    byte[] content2 = Files.readAllBytes(file2);
    return Arrays.equals(content1, content2);
}
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

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

2. 文件摘要(哈希值)比较

计算文件的哈希值(如 MD5、SHA-256 等),然后比较两个文件的哈希值。如果哈希值相同,则可以认为文件内容相同。这种方法适用于大文件,因为只需要比较哈希值而不是整个文件内容。

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

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

    byte[] file1Hash = md5.digest(input1);
    byte[] file2Hash = md5.digest(input2);

    return MessageDigest.isEqual(file1Hash, file2Hash);
}

3. FileChannel

通过使用 FileChannel 来逐块读取文件内容,然后逐块比较读取的内容。这种方法避免了每次读取数据时的数组复制。

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

        long size1 = channel1.size();
        long size2 = channel2.size();

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

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

        while (channel1.read(buffer1) != -1) {
            buffer1.flip();
            channel2.read(buffer2);
            buffer2.flip();

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

            buffer1.clear();
            buffer2.clear();
        }

        return true;
    }
}

在上述代码中,我们打开两个文件的 FileChannel,然后按照指定的缓冲区大小(例如8192字节)逐块读取两个文件的内容,并进行比较。如果任何一块内容不相等,则立即返回 false 表示文件内容不同。如果整个文件的内容都比较完毕且没有发现不同之处,则返回 true 表示文件内容相同。

请注意,这种方法适用于大文件,因为它可以避免一次性加载整个文件到内存中,而是按块逐个比较文件内容。根据具体需求和性能要求,你可以调整缓冲区大小以优化比较速度。

4. 文件元数据比较

比较文件的元数据,包括文件名、文件大小、修改时间等。这种方法快速简单,适用于需要快速确定文件是否相同的场景。

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;

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

    return attrs1.size() == attrs2.size() &&
           attrs1.lastModifiedTime().equals(attrs2.lastModifiedTime());
}

5. Apache Commons IO 库

使用 Apache Commons IO 库中的FileUtils类提供的 contentEquals() 方法来比较两个文件的内容是否相同。

import org.apache.commons.io.FileUtils;

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

6. Hutool 库

import cn.hutool.core.io.FileUtil;

public boolean areFilesEqual(File file1, File file2) throws IOException {
    FileUtil.contentEquals(file1,file2)
}
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值