2017-03-16 80 views
-4

场景:我想将我的输入csv文件与标准(模板)csv文件进行比较。这样,列headders也应与数据一起进行比较

  1. 一个std交易文件(CSV)将在那里(file1.csv)
  2. 其他文件(file2.csv)承担其从其他模块的输出,

任务:文件1 &文件2应该匹配(headders &数据)都shuld匹配,

+0

不错,你的问题是什么? –

+0

你现在的代码在哪里? –

+1

当你说匹配时,你的意思是文件1的内容等于文件2的内容? – anacron

回答

0

您可以检查使用哈希文件的平等。

如果两个文件的哈希值匹配,然后将这些文件都完全相同。

最常见的散列技术是MD5和SHA1。它适用于大多数常见目的。 (除非你使用它们进行加密或安全的目的!)

你的JRE自带java.security.MessageDigest类,提供了散列。

这里是一个示例代码,您可以使用:

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; 
    } 

} 

希望这有助于!

+0

哎,,,感谢ü这么多 –

+0

如果这个答案帮助你,你可以upvote和/或接受它作为答案。 – anacron

相关问题