package JB2015;
import java.io.*;
public class BLG {
public static void main(String[] args) throws IOException {
File srcFile=new File("D:\\幽姿不入少年场");
File destFile=new File("D:\\jdg");
copyFloder(srcFile,destFile);
}
public static void copyFloder(File srcFile,File destFile) throws IOException {
if (srcFile.isDirectory()){ //判断源文件为目录
String srcFilename= srcFile.getName();
File newFolder=new File(destFile,srcFilename);
if (!newFolder.exists()){
newFolder.mkdir();
}
File[] listFile=srcFile.listFiles();
assert listFile != null;
for (File file:listFile){
copyFloder(file,newFolder);//递归调用
}
}
else {
File newFile=new File(destFile,srcFile.getName());
copyFile(srcFile,newFile);
}
}
public static void copyFile(File srcFile,File file) throws IOException {
BufferedInputStream bi=new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bo=new BufferedOutputStream(new FileOutputStream(file));
byte[] bytes=new byte[1024];
int len;
while ((len= bi.read(bytes))!=-1){
bo.write(bytes,0,len);
bo.flush();
}
bi.close();
bo.close();
}
}
多级文件夹的复制
最新推荐文章于 2022-05-07 08:19:19 发布