当前位置:   article > 正文

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

java对比文件是否一致

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

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;
    }
}
  • 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

在上述代码中,我们打开两个文件的 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());
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

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);
}
  • 1
  • 2
  • 3
  • 4
  • 5

6. Hutool 库

import cn.hutool.core.io.FileUtil;

public boolean areFilesEqual(File file1, File file2) throws IOException {
    FileUtil.contentEquals(file1,file2)
}
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/871619
推荐阅读
相关标签
  

闽ICP备14008679号