java文件和文件夹比较_搬砖 - java文件夹对比

public classDirDiff {public staticString getFileMD5(File file) {if (!file.isFile()) {return null;

}

MessageDigest digest= null;byte buffer[] = new byte[1024];intlen;try (FileInputStream in = newFileInputStream(file);){

digest= MessageDigest.getInstance("MD5");while ((len = in.read(buffer, 0, 1024)) != -1) {

digest.update(buffer,0, len);

}

}catch(Exception e) {

e.printStackTrace();return null;

}return new BigInteger(1, digest.digest()).toString(16);

}public static Map getDirMD5(File file, booleanlistChild) {if (!file.isDirectory()) {return null;

}

Map map = new HashMap();

String md5;

File files[]=file.listFiles();for(File f : files) {if (f.isDirectory() &&listChild) {

map.putAll(getDirMD5(f, listChild));

}else{

md5=getFileMD5(f);if (md5 != null) {

map.put(f.getPath(), md5);

}

}

}returnmap;

}public static voidmain(String[] args) {

String dir1= "C:\\IDOS\\dev\\automation\\verification\\today_day_end_files\\BIS2_baseinfo";

String dir2= "C:\\IDOS\\dev\\automation\\verification\\yesterday_day_end_files\\BIS2_baseinfo";

Map map = getDirMD5(new File(dir1), true);

Map map2 = getDirMD5(new File(dir2), true);

Iterator it =map.keySet().iterator();while(it.hasNext()) {

String fullPath=it.next();

String key2=fullPath.replace(dir1, dir2);

String md5Val=map.get(fullPath);

String value2=map2.remove(key2);if (value2 == null) {

System.out.println("File is NOT exist! -----> " +key2);

}else if (!md5Val.equals(value2)) {

System.out.println("File content is different! ==> " +fullPath);

}

}

it=map2.keySet().iterator();while(it.hasNext()) {

String key=it.next();

String key2=key.replace(dir2, dir1);

System.out.println("File is NOT exist! -----> " +key2);

}

}

public String dirDiff(String orgPath, String otherPath, String... expectedFileList) {

System.out.println("orgPath :" + orgPath);

System.out.println("otherPath :" + otherPath);

List msgs = new ArrayList<>();

File orgFile = new File(orgPath);

File otherFile = new File(otherPath);

if (!orgFile.isDirectory() || !otherFile.isDirectory()) {

return String.format("Comparison is invalid between %s and %s", orgFile.getName(), otherFile.getName());

}

Map map = getDirMD5(orgFile, true);

Map map2 = getDirMD5(otherFile, true);

Iterator it = map.keySet().iterator();

while (it.hasNext()) {

String fullPath = it.next();

String key2 = fullPath.replace(orgPath, otherPath);

String md5Val = map.get(fullPath);

String value2 = map2.remove(key2);

if (value2 == null) {

msgs.add("File is NOT exist! ----->  " + key2);

} else if (!md5Val.equals(value2)) {

msgs.add("File content is different! ==> " + fullPath);

}

}

it = map2.keySet().iterator();

while (it.hasNext()) {

String fullPath = it.next();

String key2 = fullPath.replace(otherPath, orgPath);

System.out.println("File is NOT exist! ----->  " + key2);

msgs.add("File is NOT exist! ----->  " + key2);

}

return StringUtils.join(msgs, "\n");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值