JavaIO复制整个文件夹

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

public class Copy {
    public static void main(String[] args) throws IOException {
        String resourcePath = "D:\\img";
        String targetPath = "D:\\test02";
        copy(resourcePath,targetPath);
    }

    /**
     *  复制文件夹和其中的文件
     * @param resourcePath
     * 源路径
     * @param targetPath
     * 目标路径
     * @throws IOException
     */
    public static void copy(String resourcePath, String targetPath) throws IOException {
        //源文件
        File resource = new File(resourcePath);
        //目标文件
        File target = new File(targetPath);
        //源根目录
        String resourceAbsolutePath = resource.getAbsolutePath();
        //目标根目录
        String targetAbsolutePath = target.getAbsolutePath();
        //目标目录不存在 创建目录
        if (!target.exists()){
            target.mkdirs();
        }
        //源目录下的文件
        File[] resourceFiles = resource.listFiles();
        //遍历根路径下所有元素
        if (resourceFiles.length != 0){

            for (int i = 0; i < resourceFiles.length; i++){
                //当前文件的根路径
                String absolutePath = resourceFiles[i].getAbsolutePath();
                //当前文件的相对路径
                String relativePath = absolutePath.substring(resourceAbsolutePath.length());
                //拼接目标文件路径
                String tempTargetPath = targetAbsolutePath + relativePath;
                //是文件直接复制
                if (resourceFiles[i].isFile()){
                    //复制文件
                    copyFile(absolutePath, tempTargetPath);
                }else{
                    //文件夹

                    //文件夹下面没有文件
                    //创建文件夹
                    if(resourceFiles[i].listFiles().length ==0){
                        File targetFile = new File(tempTargetPath);
                        if (!targetFile.exists()){
                            targetFile.mkdirs();
                        }

                    }

                    //文件夹下有文件
                    //再次调用copy
                    copy(resourceFiles[i].getAbsolutePath(),tempTargetPath);

                }



            }
        }

    }

    /**
     * 复制文件
     * @param resourceFilePath
     * 源文件路径
     * @param targetFilePath
     * 目标文件路径
     * @throws IOException
     */
    public static void copyFile(String resourceFilePath, String targetFilePath) throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        File targetFile = new File(targetFilePath);

        File parentPath = new File(targetFile.getParent());

        //文件父路径不存在创建文件路径
        if (!parentPath.isDirectory()){
            parentPath.mkdirs();
        }
        try {
            fis = new FileInputStream(resourceFilePath);
            fos = new FileOutputStream(targetFilePath);
            int count = -1;
            byte[] bytes = new byte[1024];
            //读
            while( (count = fis.read(bytes)) != -1){
                //写
                fos.write(bytes, 0, count);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }  finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            if (fos != null) {
                try {
                    fos.flush();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值