比较两个文件夹下不同的文件-工具

比较两个文件夹下的文件是否一样,有工具太麻烦了,写了个小工具:

package com.highersoft;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

public class CompareFile {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Set<String> fold1=new HashSet<String>();
        String fold1Path="/xx1/lib";
        File f1[]=new File(fold1Path).listFiles();
        for(File f:f1) {
            fold1.add(f.getName()+f.length());
        }
        
        Set<String> fold2=new HashSet<String>();
        String fold2Path="/xx2/lib";
        File f2[]=new File(fold2Path).listFiles();
        for(File f:f2) {
            fold2.add(f.getName()+f.length());
        }
        
        Set<String> same=new HashSet<String>();
        for(String f:fold1) {
            if(fold2.contains(f)) {
                same.add(f);
            }
        }
        for(String f:fold2) {
            if(fold1.contains(f)) {
                same.add(f);
            }
        }
        
        
        for(String s:same) {
            fold1.remove(s);
            fold2.remove(s);
        }
        System.out.println(fold1Path+"多余文件:");
        System.out.println(fold1);
        System.out.println(fold2Path+"多余文件:");
        System.out.println(fold2);
        
        
    }

}

文/中中 浏览次数:0次   2019-07-09 11:43:10

相关阅读


评论: