If the probability of false positives has to be zero, as opposed to "lower than the probability you will be struck by lightning," then no hash algorithm at all can be used; you must compare the files byte by byte.
For what it's worth, if you can use third-party libraries, you can use Guava to compare two files byte-by-byte with the one-liner
Files.asByteSource(file1).contentEquals(Files.asByteSource(file2));
which takes care of opening and closing the files as well as the details of comparison.
If you're willing to accept false positives that are less likely than getting struck by lightning, then you could do
Files.hash(file, Hashing.sha1()); // or md5(), or sha256(), or...
which returns a HashCode
, and then you can test that for equality with the hash of another file. (That version also deals with the messiness of MessageDigest
, of opening and closing the file properly, etcetera.)