Java如何判断一个文件(夹)被重命名过,除了使用md5码校验?【版本控制】

背景:做版本控制Java版  如题!

使用File的lastModify方法是不行的,又不想使用md5码来校验,不使用md5来判断是因为md5算法运算大文件时耗时间。况且内容不改变,只改变文件名。


后来使用lastModify+文件的绝对路径来作为依据判断,我做了2个Map来映射,新map和旧map不匹配则会产生新增和删除这2个结果。
这样用来判断文件还是可以的  但是文件夹就不行了。如果文件夹下面还有文件(夹),也会被误认为重命名。

有什么其他解决办法吗?或者代码应该如何修改更加好?

代码如下:

public class Main {
	public Map<String,Long> map = new HashMap<String, Long>();
	public String baseFilePath;

	public static void main(String[] args) throws IOException, InterruptedException {
		String baseFilePath = "D:\\md5";
		Main m = new Main(baseFilePath);
		Map<String, Long> oldMap = m.getFileInfo(baseFilePath);
		//很长时间过去了
		//-------------------各种操作:文件(夹)或修改或删除或新增----------------
		File oldFile = new File(baseFilePath+"\\ddd.txt");//删除
		oldFile.delete();
		
		File newFile = new File(baseFilePath+"\\new.txt");//新增
		newFile.createNewFile();
		
		File olddir = new File(baseFilePath+"\\test");//重命名
		File newdir = new File(baseFilePath+"\\test2");
		olddir.renameTo(newdir);
		//-------------------操作结束---------------------------------------
		
		Main m2 = new Main(baseFilePath);
		Map<String, Long> newMap = m2.getFileInfo(baseFilePath);
		Map<String, String> resultMap = m.getModifyInfo(oldMap, newMap);
		List<String> resultKeys = new ArrayList<String>(resultMap.keySet());
		for(String resultKey:resultKeys){
			String resultValue = resultMap.get(resultKey);
			System.out.println(resultKey+"---"+resultValue);
		}
	}
	
	public Main(String baseFilePath){
		this.baseFilePath = baseFilePath;
	}
	
	/**
	 * 循环得到目录下的名字和modify时间
	 * @param baseFile 根目录
	 * @return Map<String, Long>  相对baseFilePath的路径  以及 最后修改时间
	 */
	public Map<String, Long> getFileInfo(String baseFile){
		FileOperate fo = new FileOperate();
		File[] files = fo.getFileList(baseFile);
		for(File file:files){
			map.put(file.getAbsolutePath().substring(baseFilePath.length()), file.lastModified());//相对baseFilePath的路径  以及 最后修改时间
			if(file.isDirectory()){
				getFileInfo(file.getAbsolutePath());//递归
			}
		}
		return map;
	}
	
	/**
	 * 比较目录信息(返回变动的目录信息)
	 * @param oldMap
	 * @param newMap
	 * @return 存放键值对:键是目录   值是类型(如文件内容修改,文件(夹)删除,文件(夹)添加)  注:文件(夹)重命名属于删除、添加
	 */
	public Map<String, String> getModifyInfo(Map<String, Long> oldMap,Map<String, Long> newMap){
		Map<String, String> resultMap = new HashMap<String, String>();
		List<String> oldKeysList = new ArrayList<String>(oldMap.keySet());
		List<String> newKeysList = new ArrayList<String>(newMap.keySet());
		for(String oldKey:oldKeysList){
			long oldValue = oldMap.get(oldKey);
			if(newKeysList.contains(oldKey)){//包含此文件(夹)
				long newValue = newMap.get(oldKey);
				if(oldValue==newValue){//文件(夹)没修改
					//continue;
				}else{//文件被修改
					resultMap.put(oldKey, "修改");
				}
			}else{//不包含  删除
				resultMap.put(oldKey, "删除");
			}
		}
		for(String newKey:newKeysList){
			if(oldKeysList.contains(newKey)){
				//continue;
			}else{//新增
				resultMap.put(newKey, "新增");
			}
		}
		return resultMap;
	}
}


 lastModiy什么时候生效?

对于文件,重命名不影响lastModify的值,只有修改 了其内容才会改变。

对于文件夹而已,重命名文件夹,那么lastModify是不会变的,但是其上级目录的文件夹的lastModify发现改变了。

 

 

 

 

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值