文件夹copy

import java.io.*;

public class CopyFiles {
    public static void main(String[] args) throws Exception {
        String src = "E:\\java\\workspace\\Day01\\sources\\a";//源路径
        String tar = src+1;//目标路径
        copyFolder(src,tar);//拷贝文件夹方法
    }

    private static void copyFolder(String srcFolder, String tarFolder) throws Exception {
        File tar = new File(tarFolder);
        if(!tar.exists()){
            tar.mkdir();
        }
        File src = new File(srcFolder);
        File[] srcFiles = src.listFiles();//遍历源文件
        for (File file:srcFiles
             ) {
            if(file.isFile()){//如果是文件则拷贝
                String tarPath = tarFolder+"\\"+file.getName();
                copyFile(file,tarPath);
            }
            else {//如果不是文件,则新建文件夹,把当前文件拷贝到文件夹
                //新建文件夹
                String tarPath = tarFolder+"\\"+file.getName();
                copyFolder(file.getAbsolutePath(),tarPath);
            }
        }
    }

    private static void copyFile(File srcFile, String tarPath) throws Exception {
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(tarPath);
        int len = 0;
        byte[] temp = new byte[1024];
        while((len=in.read(temp))!=-1){
            out.write(temp,0,len);
            out.flush();
        }
        out.close();
        in.close();
    }

}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值