马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码:
package cn.xiaohong;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/*
* 复制多级目录
*/
public class DiGuiCopyFolder {
public static void main(String[] args) {
// 封装源文件夹对象
File srcFolder = new File("E:/aa");
// 封装目标文件夹对象
File deskFolder = new File("D:/");
// 创建一个拷贝文件夹的方法
copyFolder(srcFolder, deskFolder);
System.out.println("拷贝成功");
}
private static void copyFolder(File srcFolder, File deskFolder) {
// 源文件是否是标准文件
if (srcFolder.isFile()) {
//是源文件就拷贝
//创建和源文件同样名字的文件
File deskFile = new File(deskFolder, srcFolder.getName());
copyFile(srcFolder, deskFile);
} else {
//如果不是就是文件夹
//在目标文件夹中创建同样名字的子文件夹
deskFolder = new File(deskFolder, srcFolder.getName());
deskFolder.mkdir();
//获取子文件夹对象
File[] files = srcFolder.listFiles();
//遍历子文件对象,再进行拷贝文件夹
for (File srcFile : files) {
copyFile(srcFile, deskFolder);
}
}
}
public static void copyFile(File srcFile, File deskFile) {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(deskFile));) {
byte[] buf = new byte[1024];
int len = -1;
while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
报错:
java.io.FileNotFoundException: D:\aa (拒绝访问。)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at cn.xiaohong.DiGuiCopyFolder.copyFile(DiGuiCopyFolder.java:51)
at cn.xiaohong.DiGuiCopyFolder.copyFolder(DiGuiCopyFolder.java:43)
at cn.xiaohong.DiGuiCopyFolder.main(DiGuiCopyFolder.java:21)
java.io.FileNotFoundException: D:\aa (拒绝访问。)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at cn.xiaohong.DiGuiCopyFolder.copyFile(DiGuiCopyFolder.java:51)
at cn.xiaohong.DiGuiCopyFolder.copyFolder(DiGuiCopyFolder.java:43)
at cn.xiaohong.DiGuiCopyFolder.main(DiGuiCopyFolder.java:21)
java.io.FileNotFoundException: E:\aa\bb (拒绝访问。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at cn.xiaohong.DiGuiCopyFolder.copyFile(DiGuiCopyFolder.java:50)
at cn.xiaohong.DiGuiCopyFolder.copyFolder(DiGuiCopyFolder.java:43)
at cn.xiaohong.DiGuiCopyFolder.main(DiGuiCopyFolder.java:21)
java.io.FileNotFoundException: D:\aa (拒绝访问。)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at cn.xiaohong.DiGuiCopyFolder.copyFile(DiGuiCopyFolder.java:51)
at cn.xiaohong.DiGuiCopyFolder.copyFolder(DiGuiCopyFolder.java:43)
at cn.xiaohong.DiGuiCopyFolder.main(DiGuiCopyFolder.java:21)
java.io.FileNotFoundException: D:\aa (拒绝访问。)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at cn.xiaohong.DiGuiCopyFolder.copyFile(DiGuiCopyFolder.java:51)
at cn.xiaohong.DiGuiCopyFolder.copyFolder(DiGuiCopyFolder.java:43)
at cn.xiaohong.DiGuiCopyFolder.main(DiGuiCopyFolder.java:21)
拷贝成功
大伙来帮我看看
43行代码copyFile(srcFile, deskFolder);改成copyFolder(srcFile,deskFolder);还有50行和51行代码这里括号和大括号有点乱,需要调整下
|