Compare two files in Java. Code to check contents of two files are equal or not.

The following code accepts two input files from the command prompt and compares the contents of the two files and returns whether the two files are equal or not. In the below example, we have used two methods to compare two files.

Ist Method : In this method, we are comparing the two files by reading the content of the files using byte array. The first condition, we need to check is that whether the length of both files are equal or not. If length of the two files are not equal, then files are not equal else we can continue to compare files using bytes. We are reading 80 bytes at a time in both files and checking if they are equal or not, this continues until the end of file is reached. During the loop, anytime the condition Arrays.equals(b1,b2) (b1 & b2 are byte array) may fail, then both files are not equal. Then no need to continue until the end of file is reached. The in equal bytes are printed by converting to string (if length of both files are equal) and returns “both files are not equal”.

IInd method :  In this method, we are going to calculate md5 hash value of both files using MessageDigest class. If the hash values of both files are equal, then both file’s contents are equal otherwise not equal. As we know, MD5 is not collision resistant, so there is no guarantee that it won’t generate the same output for the two different inputs and this method may be little bit inefficient because it always reads the entire files, even if there is a difference in the starting byte of the files.

package net.javaonline;
import java.util.*;

public class FileCompare {

	public static void main(String args[])
		if(args.length != 2)    throw (new RuntimeException("Usage : java FileCompare fileName1 fileName2"));
		String fileName1=args[0];
		String fileName2=args[1];
		String hashValue="";
		String hashValue1="";
	//First Method
		if (CompareFilesbyByte(fileName1,fileName2)==true) 
			System.out.println("No Difference encountered  using CompareFilesbyByte method");
		else System.out.println("Both Files are not equal using CompareFilesbyByte method");
	}catch (IOException e)

	//IInd Method
		hashValue =MD5HashFile(fileName1);
		hashValue1 =MD5HashFile(fileName2);
		if (hashValue.equals(hashValue1)) System.out.println("No Difference encountered using method MD5Hash"); else System.out.println("Both Files are not equal  using MD5Hash");
	catch (Exception e)

	//reading bytes and compare
	public static boolean CompareFilesbyByte(String file1, String file2) throws IOException
		File f1=new File(file1);
		File f2=new File(file2);
		FileInputStream fis1 = new FileInputStream (f1);
		FileInputStream fis2 = new FileInputStream (f2);
		if (f1.length()==f2.length())
				int n=0;
				byte[] b1;
				byte[] b2;
				while ((n = fis1.available()) > 0) {
					if (n>80) n=80;
					b1 = new byte[n];
					b2 = new byte[n];;;
					if (Arrays.equals(b1,b2)==false)
							System.out.println(file1 + " :\n\n " + new String(b1));
							System.out.println(file2 + " : \n\n" + new String(b2));
							return false;
		else return false;  // length is not matched. 
		return true;

	public static String MD5HashFile(String filename) throws Exception {
		  byte[] buf = ChecksumFile(filename);
		  String res = "";
	  for (int i = 0; i < buf.length; i++) {
		  res+= Integer.toString((buf[i] & 0xff) + 0x100, 16).substring(1);
	  return res;

	 public static byte[]  ChecksumFile(String filename) throws Exception {
	  InputStream fis = new FileInputStream(filename);
	  byte[] buf = new byte[1024];
	  MessageDigest complete = MessageDigest.getInstance("MD5");
	  int n;
	  do {
	   if (n > 0) {
	    complete.update(buf, 0, n);
	  } while (n != -1);
	  return complete.digest();


Leave a Reply