Java-文件夹复制粘贴

package file;

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

/**
 * TODO
 * 文件复制粘贴
 * @author
 * @date 2020/11/3 11:31
 */
public class FileIODemo {
    public static void main(String[] args) throws Exception{
        File file = new File("E:\\test\\1");
        String newFile = "E:\\test\\2";
        traverse(file, newFile);
    }

    public static void copy (File file,String newFiles) throws IOException {
        File file1 = new File(newFiles);
            FileInputStream fis = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(file1);
            byte[] buffer = new byte[1024*8];
            while (fis.read(buffer) != -1){
                fos.write(buffer, 0, (int) file.length());
            }
            fos.flush();
            fos.close();
            fis.close();
    }

    //遍历
    public static void traverse(File file,String newFiles) throws Exception{
        String[] files = file.list();
        if (files != null){
            for (String str : files){
                String tempFile = file + "\\" + str;
                String tempNewFile = newFiles + "\\" + str;
                File file1 = new File(tempFile);
                File file2 = new File(tempNewFile);
                if (file1.isDirectory()){
                    file2.mkdirs();
                    traverse(file1, tempNewFile);
                }else {
                    copy(file1, tempNewFile);
                }
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值