Java 判断两个文件是否相同

Posted 楼兰胡杨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 判断两个文件是否相同相关的知识,希望对你有一定的参考价值。

    使用Java 如何判断两个文件是否相同呢?我的做法是

(1)先比较两个文件内容的长度;

(2)在长度相同的情况下,再比较两个文件的MD5值。

     需要引入的Jar 包:

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.11</version>
        </dependency>

     先提供一个验证两个文件字节流是否一致的函数:

 /**
     * 验证两个文件字节流是否相等
     * @return boolean true 相等
     * @throws IOException
     */
    private static boolean isSameFiles(byte[] fileByte1, byte[] fileByte2) {
        String firstFileMd5 = DigestUtils.md5Hex(fileByte1);
        String secondFileMd5 = DigestUtils.md5Hex(fileByte2);
        if (firstFileMd5.equals(secondFileMd5)) {
            System.out.println("---- equals ------ md5 " + firstFileMd5);
            return true;
        } else {
            System.out.println(firstFileMd5 + " is firstFileMd5 ++ unequal ++ secondFileMd5 = " + secondFileMd5);
            return false;
        }
    }

1 校验base64 位字符串形式的文件是否相等

      直接把字符串转成字节数组,然后调用isSameFiles函数。

public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String img = "iVBORw0KGgoAAAANSUhEUgAAApsAAAEPCAYAAAANjQ+sAAAOqUlEQVR4Xu3WMREAAAgDMerfNCp+CwI65Bh+5wgQIECAAAECBAhEAot2zRIgQIAAAQIECBA4sekJCBAgQIAAAQIEMgGxmdEaJkCAAAECBAgQEJt+gAABAgQIECBAIBMQmxmtYQIECBAgQIAAAbHpBwgQIECAAAECBDIBsZnRGiZAgAABAgQIEBCbfoAAAQIECBAgQCATEJsZrWECBAgQIECAAAGx6QcIECBAgAABAgQyAbGZ0RomQIAAAQIECBAQm36AAAECBAgQIEAgExCbGa1hAgQIECBAgAABsekHCBAgQIAAAQIEMgGxmdEaJkCAAAECBAgQEJt+gAABAgQIECBAIBMQmxmtYQIECBAgQIAAAbHpBwgQIECAAAECBDIBsZnR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AgQCATEJsZrWECBAgQIECAAAGx6QcIECBAgAABAgQyAbGZ0RomQIAAAQIECBAQm36AAAECBAgQIEAgExCbGa1hAgQIECBAgAABsekHCBAgQIAAAQIEMgGxmdEaJkCAAAECBAgQEJt+gAABAgQIECBAIBMQmxmtYQIECBAgQIAAAbHpBwgQIECAAAECBDIBsZnRGiZAgAABAgQIEBCbfoAAAQIECBAgQCATEJsZrWECBAgQIECAAAGx6QcIECBAgAABAgQyAbGZ0RomQIAAAQIECBAQm36AAAECBAgQIEAgExCbGa1hAgQIECBAgAABsekHCBAgQIAAAQIEMgGxmdEaJkCAAAECBAgQEJt+gAABAgQIECBAIBMQmxmtYQIECBAgQIAAAbHpBwgQIECAAAECBDIBsZnRGiZAgAABAgQIEBCbfoAAAQIECBAgQCATEJsZrWECBAgQIECAAAGx6QcIECBAgAABAgQyAbGZ0RomQIAAAQIECBAQm36AAAECBAgQIEAgExCbGa1hAgQIECBAgAABsekHCBAgQIAAAQIEMgGxmdEaJkCAAAECBAgQEJt+gAABAgQIECBAIBMQmxmtYQIECBAgQIAAAbHpBwgQIECAAAECBDIBsZnRGiZAgAABAgQIEBCbfoAAAQIECBAgQCATEJsZrWECBAgQIECAAAGx6QcIECBAgAABAgQyAbGZ0RomQIAAAQIECBAQm36AAAECBAgQIEAgExCbGa1hAgQIECBAgAABsekHCBAgQIAAAQIEMgGxmdEaJkCAAAECBAgQEJt+gAABAgQIECBAIBMQmxmtYQIECBAgQIAAAbHpBwgQIECAAAECBDIBsZnRGiZAgAABAgQIEBCbfoAAAQIECBAgQCATEJsZrWECBAgQIECAAAGx6QcIECBAgAABAgQyAbGZ0RomQIAAAQIECBAQm36AAAECBAgQIEAgExCbGa1hAgQIECBAgAABsekHCBAgQIAAAQIEMgGxmdEaJkCAAAECBAgQEJt+gAABAgQIECBAIBMQmxmtYQIECBAgQIAAAbHpBwgQIECAAAECBDIBsZnRGiZAgAABAgQIEBCbfoAAAQIECBAgQCATEJsZrWECBAgQIECAAAGx6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AAQIECBAQm36AAAECBAgQIEAgExCbGa1hAgQIECBAgAABsekHCBAgQIAAAQIEMgGxmdEaJkCAAAECBAgQEJt+gAABAgQIECBAIBMQmxmtYQIECBAgQIAAAbHpBwgQIECAAAECBDIBsZnRGiZAgAABAgQIEBCbfoAAAQIECBAgQCATEJsZrWECBAgQIECAAAGx6QcIECBAgAABAgQyAbGZ0RomQIAAAQIECBAQm36AAAECBAgQIEAgExCbGa1hAgQIECBAgAABsekHCBAgQIAAAQIEMgGxmdEaJkCAAAECBAgQEJt+gAABAgQIECBAIBMQmxmtYQIECBAgQIAAAbHpBwgQIECAAAECBDIBsZnRGiZAgAABAgQIEBCbfoAAAQIECBAgQCATEJsZrWECBAgQIECAAAGx6QcIECBAgAABAgQyAbGZ0RomQIAAAQIECBAQm36AAAECBAgQIEAgExCbGa1hAgQIECBAgAABsekHCBAgQIAAAQIEMgGxmdEaJkCAAAECBAgQEJt+gAABAgQIECBAIBMQmxmtYQIECBAgQIAAAbHpBwgQIECAAAECBDIBsZnRGiZAgAABAgQIEBCbfoAAAQIECBAgQCATEJsZrWECBAgQIECAAAGx6QcIECBAgAABAgQyAbGZ0RomQIAAAQIECBAQm36AAAECBAgQIEAgExCbGa1hAgQIECBAgAABsekHCBAgQIAAAQIEMgGxmdEaJkCAAAECBAgQEJt+gAABAgQIECBAIBMQmxmtYQIECBAgQIAAAbHpBwgQIECAAAECBDIBsZnRGiZAgAABAgQIEBCbfoAAAQIECBAgQCATEJsZrWECBAgQIECAAAGx6QcIECBAgAABAgQyAbGZ0RomQIAAAQIECBAQm36AAAECBAgQIEAgExCbGa1hAgQIECBAgAABsekHCBAgQIAAAQIEMgGxmdEaJkCAAAECBAgQEJt+gAABAgQIECBAIBMQmxmtYQIECBAgQIAAAbHpBwgQIECAAAECBDIBsZnRGiZAgAABAgQIEBCbfoAAAQIECBAgQCATEJsZrWECBAgQIECAAAGx6QcIECBAgAABAgQyAbGZ0RomQIAAAQIECBAQm36AAAECBAgQIEAgExCbGa1hAgQIECBAgAABsekHCBAgQIAAAQIEMgGxmdEaJkCAAAECBAgQEJt+gAABAgQIECBAIBMQmxmtYQIECBAgQIAAAbHpBwgQIECAAAECBDKBB16TARB6VWp0AAAAAElFTkSuQmCC";

        String imgReal = "iVBOI=";

        BASE64Decoder decoder = new BASE64Decoder();
        if (isBase64(img)) {
            System.out.println("img is base 64");
        }
        if (isBase64(imgReal)) {
            System.out.println("imgReal is base 64");
        }
        byte[] imageByteArray = null;
        try {
            imageByteArray = decoder.decodeBuffer(img);
            System.out.println(isSameFiles(imageByteArray, decoder.decodeBuffer(imgReal)));
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(chechFile());
    }
/**
 * 校验字符串是否为base64 的
 *
 * @param str
 * @return
 */

private static boolean isBase64(String str) {
    String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
    return Pattern.matches(base64Pattern, str);
}
 

2 校验两个文件是否相等

     读取文件后,将其转换成字节流:

    /**
     * 检查两个文件是否相等,假设 C:\\Users\\Pictures目录下有两个图片文件
     * @return boolean true 相等
     * @throws IOException
     */
    private static boolean chechFile() throws IOException {
        File file1 = new File("C:\\\\Users\\\\Pictures\\\\test1.jpg");
        File file2 = new File("C:\\\\Users\\\\Pictures\\\\test2.jpg");
        if (file1.length() != file2.length()) {
            System.out.println("+++++++++ unequal +++++++++++++");
            return false;
        }
        InputStream fileStream1 = new FileInputStream(file1);
        InputStream fileStream2 = new FileInputStream(file2);
//        InputStream 转 byte[]
        byte[] imageByteArray = new byte[fileStream1.available()];
        System.out.println(isSameFiles(imageByteArray, new byte[fileStream2.available()]));
        return true;
    }

 

     以上方法验证小文件妥妥的,大文件请移步:《JAVA MD5 验证大文件(改)

Reference

https://blog.csdn.net/wxz980927155/article/details/82257225

https://www.cnblogs.com/DylanZ/p/6269042.html

以上是关于Java 判断两个文件是否相同的主要内容,如果未能解决你的问题,请参考以下文章

Java判断两个List是否相同

如何使用 Perl 判断两个文件的内容是不是相同?

Java判断两个对象是否相等的规则

判断两个文件是否相同

python代码判断两棵二叉树是否相同

javascript常用代码片段