-4

scenario:

  1. a std Transaction file (csv) will be there (file1.csv)
  2. other file( file2.csv) assume its a output from other module,

TASK: file1 & file2 should match (headers & data) both should match,

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

You can check for equality of files using Hashes.

If the hash values of two files match, then the files are exactly same.

Most common hashing techniques are MD5 and SHA1. It would be suitable for most common purposes. (Unless you are using them for cryptographic or security purposes!)

Your JRE comes with the java.security.MessageDigest class that provides hashing.

Here is a sample code that you can use:

public class HashCheck {

    public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
        byte[] file1Contents = Files.readAllBytes(Paths.get("C:/path/to/file1.csv"));
        byte[] file2Contents = Files.readAllBytes(Paths.get("C:/path/to/file2.csv"));

        String hashtext1 = computeHash(file1Contents);
        String hashtext2 = computeHash(file2Contents);
        System.out.println(hashtext1);
        System.out.println(hashtext2);
        System.out.println(hashtext1.equals(hashtext2));
    }

    public static String computeHash(String input) throws NoSuchAlgorithmException {
        return computeHash(input.getBytes());
    }

    public static String computeHash(byte[] input) throws NoSuchAlgorithmException {
        MessageDigest hasher = java.security.MessageDigest.getInstance("MD5"); //MD5 or SHA1
        hasher.reset();
        hasher.update(input);
        byte[] digest = hasher.digest();
        BigInteger bigInt = new BigInteger(1, digest);
        String hashtext = bigInt.toString(16); // The hashes are base-16 numbers
        // Now we need to zero pad it if you actually want the full 32 chars.
        while(hashtext.length() < hasher.getDigestLength() ){
          hashtext = "0"+hashtext;   
        }
        return hashtext;
    }

}

Hope this helps!

anacron
  • 6,443
  • 2
  • 26
  • 31