Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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

like image 668
Dmitrii Pisarenko Avatar asked Apr 02 '14 17:04

Dmitrii Pisarenko


People also ask

How do I compare two binary files?

Use the command cmp to check if two files are the same byte by byte. The command cmp does not list differences like the diff command. However it is handy for a fast check of whether two files are the same or not (especially useful for binary data files).

Can we compare two files in Java?

Two file paths can be compared lexicographically in Java using the method java. io. File. compareTo().

Can diff compare binary files?

diff determines whether a file is text or binary by checking the first few bytes in the file; the exact number of bytes is system dependent, but it is typically several thousand. If every byte in that part of the file is non-null, diff considers the file to be text; otherwise it considers the file to be binary.


1 Answers

Are third-party libraries fair game? Guava has Files.equal(File, File). There's no real reason to bother with hashing if you don't have to; it can only be less efficient.

like image 157
Louis Wasserman Avatar answered Sep 19 '22 17:09

Louis Wasserman