评论详情
自来也 ID:QJf8+PPp

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

public class Demo {

    public static void main(String[] args) {
        copy(new File(D:\\desktop\\temp), new File(D:\\desktop\\abc));

    }
    
    public static void copy(File file, File target) {
        target = new File(target, file.getName());
        if (!target.exists()) target.mkdirs();
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                if (f.isDirectory()) copy(f, target);
                else copyFile(f, target);
            }
        } else {
            copyFile(file, target);
        }
    }

    public static void copyFile(File file, File directory) {
        try {
            FileInputStream input = new FileInputStream(file);
            FileOutputStream output = new FileOutputStream(new File(directory, file.getName()));
            byte[] buff = new byte[8192];
            int len = -1;
            while ((len = input.read(buff))  -1) {
                output.write(buff);
            }
            input.close();
            output.close();
        } catch (Exception e) {

        }
    }

}


java文件夹复制_java复制整个文件夹-CSDN博客 文章浏览阅读390次。使用java语言在windows操作系统下对文件夹进行复制操作。_java复制整个文件夹 https://blog.csdn.net/ganlinczz/article/details/129208877

Dec 9, 2023

回复
回复评论
0 0