What is the easiest way to check (in a unit test) whether binary files A and B are equal?
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).
Two file paths can be compared lexicographically in Java using the method java. io. File. compareTo().
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With