How can I efficiently compare two binary files for equality?
Location: http://www.jguru.com/faq/view.jsp?EID=66830 Created: Jun 7, 2000 Modified: 2000-06-07 18:11:07.065 Author: Brian O’Byrne (http://www.jguru.com/guru/viewbio.jsp?EID=38567) Question originally posed by Prabir Das (http://www.jguru.com/guru/viewbio.jsp?EID=60476 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 = inputStreamA.read(streamABlock); int bytesReadB = inputStreamB.read(st