package CollectionsFile;
import Code.Buffered;
import java.io.*;
public class CopyFoldersDemo{
public static void main(String[] args) throws IOException {
File yuanFile=new File("D:\\IDEA\\FileExample2");
File xianFile=new File("D:\\IDEA\\File");
copyFolders(yuanFile,xianFile);
}
private static void copyFolders(File yuanFile, File xianFile) throws IOException {
if (yuanFile.isDirectory()) {
String yuanName = yuanFile.getName();
File newXianFile=new File(xianFile,yuanName);
if (!newXianFile.exists()){
newXianFile.mkdir();
}
File[] listFiles = yuanFile.listFiles();
for (File file:listFiles){
copyFolders(file,newXianFile);
}
}else {
String yuanName = yuanFile.getName();
File newXianFile=new File(xianFile,yuanName);
copyFile(yuanFile,newXianFile);
}
}
private static void copyFile(File yuanFile, File xianFile) throws IOException {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(yuanFile));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(xianFile));
byte[] bys = new byte[1024];
int len;
while ((len=bis.read(bys))!=-1){
bos.write(bys);
}
bis.close();
bos.close();
}
}
复制多级文件夹
最新推荐文章于 2024-07-11 17:24:34 发布