java 比较两个目录文件差异吧差异部分文件按照相同的目录格式写入到第三个文件夹

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;


public class Cmp {
	static String change = "F:/cmp/change";
	static String local = "F:/cmp/local";
	static String svn = "F:/cmp/svn";
	static int changeLen = 0;
	public static void main(String[] args) throws Exception {
		compare(new File(local));
	}
	
	private static void compare(File file) throws Exception{
		String localFilePath = file.getAbsolutePath();
		System.out.println(localFilePath);
		String relativePath = localFilePath.substring(local.length());
		File svnFile = new File(svn,relativePath);
		File changeFile = new File(change,relativePath);
		if(file.isDirectory()){
			if(!svnFile.exists()){
				changeFile.mkdirs();
			}
			File[] childs = file.listFiles();
			if(childs != null){
				for(File child:childs){
					compare(child);
				}
			}
		}else{
			if(!svnFile.exists()){
				copy(file, changeFile);
			}else{
				if(Math.abs(file.length() - svnFile.length()) > changeLen){
					copy(file, changeFile);
				}
			}
		}
	}
	
	public static void createFolder(File src){
		String localFilePath = src.getAbsolutePath();
		String relativePath = localFilePath.substring(local.length());
		if(relativePath.length()>0 && relativePath.contains("/")){
			//注意这里的 /  有可能需要换成  \  机器差异
			relativePath = relativePath.substring(0,relativePath.lastIndexOf("/"));
			File folder = new File(change,relativePath);
			folder.mkdirs();
		}
	}
	
	public static void copy(File src,File dest) throws Exception{
		createFolder(src);
		
		FileInputStream in = new FileInputStream(src);
		byte[] buf = new byte[1024];
		FileOutputStream out = new FileOutputStream(dest);
		int len = 0;
		while((len = in.read()) > -1){
			out.write(buf,0,len);
		}
		out.flush();
		out.close();
		in.close();
	}
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值