How can I efficiently compare two binary files for equality?

Brian O'Byrne

An option which I have used, and found to be reasonably performant, is to read the files in blocks into byte arrays and use equals() to compare the blocks.
Something like this:

private final static int BLOCK_SIZE = 65536;
// vary BLOCK_SIZE to suit yourself.
// it should probably a factor or multiple of the size of a disk sector/cluster.
// Note that your max heap size may need to be adjused
//  if you have a very big block size or lots of these comparators.

// assume inputStreamA and inputStreamB are streams from your two files.
byte[] streamABlock = new byte[BLOCK_SIZE];
byte[] streamBBlock = new byte[BLOCK_SIZE];
boolean match;
do {
  int bytesReadA =;
  int bytesReadB =;
  match = ((bytesReadA == bytesReadB) && bytesReadA.equals(bytesReadB));
} while (match && (bytesReadA > -1));

It's not fancy, but I think when you want speed simple is good.
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact