java文件目录拷贝第一次运行报错

首先代码

package com.xxx.javase.FileHomework;

import java.io.*;

public class FileCopyTest {
    public static void main(String[] args) {
        //拷贝源
        File srcFile = new File("E:\\PrintScreen");
        //拷贝目标
        File destFile = new File("C:\\");
        //调用方法
        copyDir(srcFile, destFile);
    }
    private static void copyDir(File srcFile, File destFile) {
        if (srcFile.isFile()){
            //如果srcFile是个文件而不是目录,递归结束。
            FileInputStream in = null;
            FileOutputStream out = null;

            try {
                in = new FileInputStream(srcFile);

                String path = (destFile.getAbsolutePath().endsWith("\\")? destFile.getAbsolutePath() : destFile.getAbsolutePath() + "\\") + srcFile.getAbsolutePath().substring(3);
                out = new FileOutputStream(path);

                byte[] bytes = new byte[1024 * 1024];
                int readCount = 0;
                while ((readCount = in.read(bytes)) != -1){
                    out.write(bytes,0,readCount);
                }

                out.flush();

            } catch (FileNotFoundException e) {
                e.printStackTrace();

            } catch (IOException e) {
                e.printStackTrace();

            } finally {
                if (out != null){
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return;
        }
        //获取源下面的子目录
        File[] files = srcFile.listFiles();
        for (File file : files){
            if (file.isDirectory()){
                String srcDir = file.getAbsolutePath();
                String destDir = (destFile.getAbsolutePath().endsWith("\\")? destFile.getAbsolutePath() : destFile.getAbsolutePath() + "\\") + srcDir.substring(3);
                File newFile = new File(destDir);
                if (!newFile.exists()){
                    newFile.mkdirs();
                }
            }
            copyDir(file, destFile);
        }
    }
}

学习目录拷贝时,将E:\PrintScreen文件夹拷贝到C:\时

第一次运行程序出现java.io.FileNotFoundException: (系统找不到指定的路径。)报错

java.io.FileNotFoundException: C:\PrintScreen\1.jpg (系统找不到指定的路径。)
    at java.base/java.io.FileOutputStream.open0(Native Method)
    at java.base/java.io.FileOutputStream.open(FileOutputStream.java:292)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:235)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:124)
    at com.xxx.javase.FileHomework.FileCopyTest.copyDir(FileCopyTest.java:24)
    at com.xxx.javase.FileHomework.FileCopyTest.copyDir(FileCopyTest.java:70)
    at com.xxx.javase.FileHomework.FileCopyTest.main(FileCopyTest.java:12)
java.io.FileNotFoundException: C:\PrintScreen\2.jpg (系统找不到指定的路径。)
    at java.base/java.io.FileOutputStream.open0(Native Method)
    at java.base/java.io.FileOutputStream.open(FileOutputStream.java:292)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:235)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:124)
    at com.xxx.javase.FileHomework.FileCopyTest.copyDir(FileCopyTest.java:24)
    at com.xxx.javase.FileHomework.FileCopyTest.copyDir(FileCopyTest.java:70)
    at com.xxx.javase.FileHomework.FileCopyTest.main(FileCopyTest.java:12)
java.io.FileNotFoundException: C:\PrintScreen\3.jpg (系统找不到指定的路径。)
    at java.base/java.io.FileOutputStream.open0(Native Method)
    at java.base/java.io.FileOutputStream.open(FileOutputStream.java:292)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:235)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:124)
    at com.xxx.javase.FileHomework.FileCopyTest.copyDir(FileCopyTest.java:24)
    at com.xxx.javase.FileHomework.FileCopyTest.copyDir(FileCopyTest.java:70)
    at com.xxx.javase.FileHomework.FileCopyTest.main(FileCopyTest.java:12)
java.io.FileNotFoundException: C:\PrintScreen\Map集合.jpg (系统找不到指定的路径。)
    at java.base/java.io.FileOutputStream.open0(Native Method)
    at java.base/java.io.FileOutputStream.open(FileOutputStream.java:292)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:235)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:124)
    at com.xxx.javase.FileHomework.FileCopyTest.copyDir(FileCopyTest.java:24)
    at com.xxx.javase.FileHomework.FileCopyTest.copyDir(FileCopyTest.java:70)
    at com.xxx.javase.FileHomework.FileCopyTest.main(FileCopyTest.java:12)

Process finished with exit code 0

发现PrintScreen文件夹内只有部分文件拷贝成功,但子目录内所有文件全部成功

将文件夹删除后,重新运行,还是那几个文件成功拷贝,部分文件报错,拷贝失败

试着第二次运行,发现所有文件拷贝成功。

多次尝试后发现,当在C:\目录下创建好PrintScreen文件夹后运行程序,不会发生异常,第一次运行就成功,所以怀疑是否是在运行顺序上,先拷贝文件却没有拷贝PrintScreen文件夹,但分析逻辑并没有发现异常。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值