当前位置:   article > 正文

Java 判断两个文件是否相同的两种方法_java 判断 两个文件相同

java 判断 两个文件相同

昨天因为要帮师兄的忙所以看了一下如何判断两个文件内容是否相同,在这里提一下其实有很专业的工具如winmerge等,不过自己实现一下有助于学习和知识面的扩展。

第一,判读MD5值或SHA-1,以MD5为例,

// 计算文件的 MD5 值
publicstatic String getFileMD5(File file) {
     if  (!file.isFile()) {
         return  null ;
     }
     MessageDigest digest =  null ;
     FileInputStream in =  null ;
     byte  buffer[] = newbyte[ 8192 ];
     int  len;
     try  {
         digest =MessageDigest.getInstance( "MD5" );
         in =  new  FileInputStream(file);
         while  ((len = in.read(buffer)) != - 1 ) {
             digest.update(buffer,  0 , len);
         }
         BigInteger bigInt =  new  BigInteger( 1 , digest.digest());
         return  bigInt.toString( 16 );
     catch  (Exception e) {
         e.printStackTrace();
         return  null ;
     finally  {
         try  {
             in.close();
         catch  (Exception e) {
             e.printStackTrace();
         }
     }
  
}

第二,直接判读内容,代码如下:

  1. public class  IOOperation  
  2. {  
  3.     public static void main(String[] args)   
  4.     {  
  5.         FileInputStream File1 = null;  
  6.         FileInputStream File2 = null;  
  7.         BufferedReader in = null;  
  8.         String sFile;  
  9.   
  10.         if(args.length != 2)  
  11.         {  
  12.             System.out.println("The command line should be: java IOOperation testX.txt testX.txt");  
  13.             System.out.println("X should be one of the array: 1, 2, 3");  
  14.             System.exit(0);  
  15.         }  
  16.   
  17.         try  
  18.         {  
  19.             File1 = new FileInputStream(args[0]);  
  20.             File2 = new FileInputStream(args[1]);  
  21.               
  22.             try  
  23.             {  
  24.               
  25.                 if(File1.available() != File2.available())  
  26.                 {  
  27.                    //长度不同内容肯定不同  
  28.                     System.out.println(args[0] + " is not equal to " + args[1]);  
  29.                 }  
  30.                 else  
  31.                 {  
  32.                     boolean tag = true;  
  33.   
  34.                     while( File1.read() != -1 && File2.read() != -1)  
  35.                     {  
  36.                         if(File1.read() != File2.read())  
  37.                         {  
  38.                             tag = false;  
  39.                             break;  
  40.                         }  
  41.                     }  
  42.   
  43.                     if(tag == true)  
  44.                         System.out.println(args[0] + " equals to " + args[1]);  
  45.                     else  
  46.                         System.out.println(args[0] + " is not equal to " + args[1]);  
  47.                 }  
  48.             }  
  49.             catch(IOException e)  
  50.             {  
  51.                 System.out.println(e);  
  52.             }  
  53.         }  
  54.         catch (FileNotFoundException e)  
  55.         {  
  56.             System.out.println("File can't find..");  
  57.         }  
  58.         finally  
  59.         {  
  60.               
  61.             try  
  62.             {  
  63.                 if(File1 != null)  
  64.                     File1.close();  
  65.                 if(File2 != null)  
  66.                     File2.close();  
  67.             }  
  68.             catch (IOException e)  
  69.             {  
  70.                 System.out.println(e);  
  71.             }  
  72.         }  
  73.     }  

       这里因为使用了第二种方法所以直接用了main函数,代码结构可以再优化。MD5方法用到了以前没有接触到的知识,如MessageDigest、BigInteger等,个人觉得要想了解这些方法最好的方法就是读API文档。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/871646
推荐阅读
相关标签
  

闽ICP备14008679号