java 复制文件夹下所有文件及目录

/**
 * @author 12761
 * @version v1.0
 */
public class CopyFile {

    public static void copy(File file, File TargetDirectory) {
        File TargetFile = new File(TargetDirectory, file.getName());

        if (file.isDirectory()) {

            TargetFile.mkdir();
            File[] files = file.listFiles();
            for (File f : files) {
                copy(f, TargetFile);
            }
        } else {

            try (InputStream is = new FileInputStream(file);
                 OutputStream os = new FileOutputStream(TargetFile);) {

                byte[] buf = new byte[1024];
                int length = 0;
                while ((length = is.read(buf)) > 0) {
                    os.write(buf, 0, length);
                }
                System.out.println("拷贝完成!");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

//测试
public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        File file = null;
        do {
            System.out.print("请输入需要拷贝的文件的路径(原文件):");
            String originalPath = sc.nextLine();
            file = new File(originalPath);

            if (!file.exists()) {
                System.out.println("你输入的文件地址不存在,请重新输入!");
            }
        } while (!file.exists());

        System.out.print("请输入你要拷贝的目标文件的地址(目标目录):");
        String targetPath = sc.nextLine();
        File targetDirectory = new File(targetPath);
        if (!targetDirectory.isDirectory()) {
            System.out.println("你输入的不是一个目录!");
            return;
        }
        CopyFile.copy(file, targetDirectory);
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java复制文件夹下的所有文件可以使用递归方法来实现。具体步骤如下: 1. 创建一个方法,传入源文件夹和目标文件夹的路径作为参数。 2. 使用File类的listFiles()方法获取源文件夹下的所有文件和子文件夹。 3. 遍历源文件夹下的每个文件文件夹。 4. 对于文件,使用File类的字节输入流(FileInputStream)和字节输出流(FileOutputStream)来进行复制操作。首先创建输入流读取源文件内容,再创建输出流将内容写入目标文件中。 5. 对于子文件夹,递归调用该方法,将子文件夹作为源文件夹、目标文件夹路径作为参数传入。 6. 复制完成后,关闭输入流和输出流。 下面是代码示例: ```java import java.io.*; public class CopyFolder { public static void main(String[] args) { String sourceFolder = "源文件夹路径"; String targetFolder = "目标文件夹路径"; copyFiles(sourceFolder, targetFolder); } public static void copyFiles(String sourceFolder, String targetFolder) { File source = new File(sourceFolder); File[] files = source.listFiles(); if (files != null) { for (File file : files) { if (file.isFile()) { File target = new File(targetFolder + File.separator + file.getName()); try (InputStream in = new FileInputStream(file); OutputStream out = new FileOutputStream(target)) { byte[] buffer = new byte[4096]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } } catch (IOException e) { e.printStackTrace(); } } else if (file.isDirectory()) { String newSourceFolder = sourceFolder + File.separator + file.getName(); String newTargetFolder = targetFolder + File.separator + file.getName(); copyFiles(newSourceFolder, newTargetFolder); } } } } } ``` 以上代码会将源文件夹下的所有文件文件夹复制到目标文件夹中。如果源文件夹有多级子文件夹,也会被递归复制
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值