完成需求:使用IO流复制一个多级文件夹到目标路径
但是一致显示:路径找不到报错
public class CopyUp {
public static void main(String[] args) throws IOException {
File src = new File("D:\\SRP1");
File target = new File("D:\\Dev\\7");
copyAll(src,target);
}
//复制文件中的单个文件,
public static void copyFile(File srcFile, File target) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));
byte[] bys = new byte[1024];
int len;
while((len = bis.read(bys))!=-1){
bos.write(bys);
}
bis.close();
bos.close();
}
public static void copyAll(File src, File target) throws IOException {
if (src.isDirectory()) {
//在目标目录下创建名字相同的目录对象
File newFolder = new File(target, src.getName());
//判断目标真实目录是否存在,不存在就创建
if (!newFolder.exists()) {
newFolder.mkdir();
}
//遍历源目录下的所有文件或者目录
File[] files = src.listFiles();
for (File file : files) {
copyAll(file, newFolder);
}
}else {
File newFile = new File(target, src.getName());
System.out.println(newFile.getAbsoluteFile());
newFile.createNewFile();
copyFile(src,newFile);
}
}
}