首先代码
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:\时
![](https://i-blog.csdnimg.cn/blog_migrate/8fbc92c6456ecfa5a740ba77ee2461ce.png)
第一次运行程序出现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文件夹内只有部分文件拷贝成功,但子目录内所有文件全部成功
将文件夹删除后,重新运行,还是那几个文件成功拷贝,部分文件报错,拷贝失败
![](https://i-blog.csdnimg.cn/blog_migrate/3d83d7bc6b69cd520be297db3b0bbe21.png)
试着第二次运行,发现所有文件拷贝成功。
![](https://i-blog.csdnimg.cn/blog_migrate/f5581af93dcf6e7bc41d4e43f52ec870.png)
多次尝试后发现,当在C:\目录下创建好PrintScreen文件夹后运行程序,不会发生异常,第一次运行就成功,所以怀疑是否是在运行顺序上,先拷贝文件却没有拷贝PrintScreen文件夹,但分析逻辑并没有发现异常。