嵌套文件夹复制实现

最简单的文件及文件夹复制

文件及文件夹复制

递归搜索,然后复制文件
代码如下:

package com.neusoft.myMaven.example;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyFile {
    public static final String filePath = System.getProperty("user.dir") + File.separator + "mp3" + File.separator
            + "3";
    public static final String directoryPath = System.getProperty("user.dir") + File.separator + "mp3";

    public static final String copyPath = System.getProperty("user.dir") + File.separator + "mp3" + File.separator
            + "31";

    public static void copy() {
        long start = System.currentTimeMillis();
        FileUtil fUtil = new FileUtil(filePath, copyPath);
        try {
            System.out.println(fUtil.copy() ? "SUCCESS" : "ERROR");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("Copy Complete".toUpperCase() + ",USED TIME:" + (end - start));
    }
}

class FileUtil {
    private File srcFile;// 源文件路径
    private File desFile;// 目标文件路径

    public FileUtil(String src, String des) {
        this(new File(src), new File(des));
    }

    public FileUtil(File srcFile, File desFile) {
        this.srcFile = srcFile;
        this.desFile = desFile;
    }

    /**
     * @name: 递归搜索文件或者文件夹及子文件夹中的文件
     * @msg:
     * @param {type}
     * @return:
     */
    private void searchFile(File file) throws Exception {
        if (file.isDirectory()) {
            File newFile = new File(file.getPath().replace(this.srcFile.getPath(), this.desFile.getPath()));
            if (!newFile.exists()) {
                newFile.mkdirs();// 保证目标文件的文件夹存在
            }
            File[] results = file.listFiles();
            if (results != null) {
                for (int i = 0; i < results.length; i++) {
                    searchFile(results[i]);// 递归搜索
                }
            }
        } else {
            if (file.isFile()) {
                String newFilePath = file.getPath().replace(this.srcFile.getPath() + File.separator, "");
                File newFile = new File(this.desFile, newFilePath);
                this.copyFile(file, newFile);
            }
        }
    }

    /**
     * @name: 复制文件
     * @msg:
     * @param {type}
     * @return:
     */
    private boolean copyFile(File srcFileTmp, File desFileTmp) throws IOException {
        if (!this.desFile.getParentFile().exists()) {
            this.desFile.getParentFile().mkdirs();
        }

        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(srcFileTmp);
            output = new FileOutputStream(desFileTmp);
            // input.transferTo(output);//jdk1.9后提供的方法,简单方便
            // 下面这一部分和上面一句是同样的作用,用于jdk1.9以下
            int len = 0;
            byte[] data = new byte[10240];
            while ((len = input.read(data)) != -1) {
                output.write(data, 0, len);
            }
            // 此快到这里结束
            return true;
        } catch (Exception e) {
            // TODO: handle exception
            throw e;
        } finally {
            // 一定要记得关闭资源
            if (input != null) {
                input.close();
            }
            if (output != null) {
                output.close();
            }
        }
    }

    public boolean copy() throws Exception {
        if (!this.srcFile.exists()) {
            System.out.println("Source file does not exist".toUpperCase());
            return false;
        }
        try {
            this.searchFile(this.srcFile);
            return true;
        } catch (Exception e) {
            // TODO: handle exception
            throw e;
        }
    }
}

调用方法:

CopyFile.copy();

源码已经出来了,没有过多的需要说。那我就大概说一下思路吧。

思路

  1. 复制的时候,拿最难得想,无非就是嵌套文件夹。而咱们需要复制的是文件,文件夹自己新建就可以。所以第一步是找到文件位置,并取得文件路径。
  2. 取到文件路径之后就好办了,那输入流读取旧文件的内容,然后输出流写到新的文件里面就可以。
  3. 写的时候需要注意的是,目标文件夹中没有子目录,一定要先创建子目录。用到的方法是获取到子目录的路径,然后将源文件路径替换为目标文件路径,然后创建即可。

畅想

这个方法是初级复制,只是实现了复制这个操作,可以往深探索。
我这里提出来一个畅想——快速复制
原理很简单,多线程。
将文件切片,分为很多小片段,然后多线程复制。复制之后再根据切片规则重新组装。
差不多就是这样

所学所得,有任何错误还请指出,多谢

永久链接:https://neusoft.me/java/2020/05/08/513/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值