java实现文件夹增量同步复制

/**
     * 复制单个文件从srcPath到desPath
     *
     * @param srcPath
     * @param desPath
     */
    public void copyFile(String srcPath, String desPath) {
        try {
            //创建输入输出流对象
            FileInputStream fis = new FileInputStream(srcPath);
            FileOutputStream fos = new FileOutputStream(desPath);
            byte datas[] = new byte[1024 * 2];  //搬运工具
            int len = 0;
            while ((len = fis.read(datas)) != -1) {// 读取数据
                fos.write(datas, 0, len);
            }
            //释放资源
            fis.close();
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件夹A-->B前比较出A中有而B中没有的文件路径映射
     *
     * @param fileMapA
     * @param fileMapB
     */
    public Map<String, File> compoareFileMap(Map<String, File> fileMapA, Map<String, File> fileMapB) {
        Map<String, File> fileMap = new HashMap<String, File>();
        try {
            for (String path : fileMapA.keySet()) {
                if (fileMapB.get(path) == null) {
                    fileMap.put(path, fileMapA.get(path));//(相对路径,子文件路径)
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        int i = 0;
        for (String relative: fileMap.keySet()) {
            i++;
        }
        System.out.println("一共有"+i+"个文件不同,将检查文件最后修改时间");
        return fileMap;//A中有而B中没有的文件路径映射
    }

    /**
     * 根据A中有而B中没有的文件路径映射将云映射对应的文件复制到B中去
     *
     * @param fileMap
     * @param sourcePath
     * @param desFilePath 目标文件夹
     */
    public void copyFilesByMap(Map<String, File> fileMap, String sourcePath, String desFilePath,int time) {
        try {
            File desFile = new File(desFilePath);
            if (!desFile.exists()) {
                desFile.mkdirs();
            }
            for (String srcfileRelativePath : fileMap.keySet()) {
                File srcFile = fileMap.get(srcfileRelativePath);
                File srcParentFile = srcFile.getParentFile();
                String srcParentFilePath = srcParentFile.getPath();
                String parentRelativePath = srcParentFilePath.substring(sourcePath.length(), srcParentFilePath.length());
                File desParentFile = new File(desFilePath + "\\" + parentRelativePath);
                if (!desParentFile.isDirectory()) {
                    desParentFile.mkdirs();
                }

                /**
                 * 根据文件最后修改时间判定是否需要复制
                 * */
                long srcFileTime = srcFile.lastModified();
                //System.out.println(srcFileTime);
                System.out.println((System.currentTimeMillis()-srcFileTime)/60000);
                if((System.currentTimeMillis()-srcFileTime)/60000<time){//如果文件最后修改时间不足time,则复制
                    copyFile(sourcePath + srcfileRelativePath, desFilePath + srcfileRelativePath);
                    System.out.println("复制"+sourcePath + srcfileRelativePath + "--到-->" + desFilePath + srcfileRelativePath+"完毕");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取某文件夹中所有文件的路径映射
     *
     * @param file 子文件夹
     * @param fileMap 文件映射
     * @param sourceFilePath 总文件夹路径
     * @return fileMap
     */
    public Map<String, File> getFileMap(File file, Map<String, File> fileMap, String sourceFilePath) {
        try {
            for (int i = 0; i < file.listFiles().length; i++) {
                File childFile = file.listFiles()[i];
                if (!childFile.isDirectory()) {//子文件直接放入映射
                    String chileFilePath = childFile.getPath();//子文件路径
                    String relativePath = chileFilePath.substring(sourceFilePath.length(), chileFilePath.length());//子文件路径减去总文件路径的相对路径
                    fileMap.put(relativePath, childFile);
                } else {//子文件夹迭代
                    getFileMap(childFile, fileMap, sourceFilePath);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileMap;
    }
public class Test {

    public static void main(String[] args){
       FileCopyDao fileCopyDao = new FileCopyDaoImpl();
        try{
            Map<String, File> fileMap = new HashMap<String,File>();
            File file1 = new File("D:\\testspace\\thunder");
            File file2 = new File("D:\\testspace\\thunder2");
            Map<String, File> fileMap1 = fileCopyDao.getFileMap(file1,fileMap,file1.getPath());
            System.out.println("扫描"+file1+"完毕");
            fileMap = new HashMap<String,File>();
            Map<String, File> fileMap2 = fileCopyDao.getFileMap(file2,fileMap,file2.getPath());
            System.out.println("扫描"+file2+"完毕");
            Map<String,File> map = fileCopyDao.compoareFileMap(fileMap1,fileMap2);
            fileCopyDao.copyFilesByMap(map,file1.getPath(),file2.getPath(),180);//复制三个小时内新建文件
            System.out.print(1);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
IncreSync是多文件夹批量处理的文件增量备份软件,它支持两种同步方式,在线同步和离线同步。在线同步是两台联网机器之间的文件同步,通过共享访问另一台电脑;离线同步是通过U盘等第三方介质进行两台电脑之间的文件同步。IncreSync同步可以是单向同步或双向同步,可以配置是否同步新增、修改、删除和隐藏的文件,以及通过通配符,过滤部分文件。 在线同步时,连接好两台电脑,点击联机同步就可以同步文件。 离线同步时,将U盘插入源电脑,运行程序,点击源端同步文件同步到U盘,然后将U盘插入目的电脑,点击目的端同步文件同步到目的目录。 详细的使用方法,请参考系统的帮助文档。 文件同步示意图: 同步方向 按钮 联机同步 移动存储同步 ----------------------------------------------------- |源端同步 | |源端-->U盘 | | | 单向同步 |目的端同步 | |U盘-->目的端 | | | |联机同步 |源端-->目的端 | ----------------------------------------------------- |源端同步 | |U盘-->源端 | | |源端-->U盘 | | | 双向同步 |目的端同步 | |U盘-->目的端 | | |目的端-->U盘 | | | |联机同步 |源端-->目的端 | | |目的端-->源端 | ----------------------------------------------------- 注: 1、源端-->目的端 代表文件从源端同步到目的端,其他的依次类推。 2、在移动存储同步时,需要将本程序拷到U盘等第三方介质,因为临时文件会存储在程序的临时文件夹里。 3、本系统可以通过任务调度,配置为自动同步方式,需要请联系zhayaqi@hotmail.com. 如果你对系统有什么意见或建议,请您发送邮件至zhayaqi@hotmail.com,非常感谢,期待您的反馈!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值