Java: How to check that 2 binary files are same?

前端 未结 7 1293
旧时难觅i
旧时难觅i 2021-02-07 17:27

What is the easiest way to check (in a unit test) whether binary files A and B are equal?

7条回答
  •  北海茫月
    2021-02-07 18:21

    If you want to avoid dependencies you can do it using quite nicely with Files.readAllBytes and Assert.assertArrayEquals

    Assert.assertArrayEquals("Binary files differ", 
        Files.readAllBytes(Paths.get(expectedBinaryFile)), 
        Files.readAllBytes(Paths.get(actualBinaryFile)));
    

    Note: This will read the whole file so it might not be efficient with large files.

提交回复
热议问题