我是靠谱客的博主 火星上纸飞机,这篇文章主要介绍JavaIO流复制多级文件夹(递归),现在分享给大家,希望可以做个参考。

public class Demo_DuoJi {
public static void main(String[] args) throws IOException{
//创建数据源对象,路径是H:\daywork\itheima
File srcFile=new File("H:\itheima");
// 创建目的地file对象,路径是H:
File dirFile=new File("D:\");
//写方法实现文件夹的复制
copyFolder(srcFile,dirFile);
}
private static void copyFolder(File srcFile, File dirFile) throws IOException{
if(srcFile.isDirectory()){
//说明是目录
String srcFileName=srcFile.getName();
File newFolder=new File(dirFile,srcFileName);
if(!newFolder.exists()){
newFolder.mkdir();
}
File[] fileArray=srcFile.listFiles();
for (File file:fileArray){
copyFolder(file,newFolder);
}
}else{
//说明是文件,直接复制,用字节流
File newFile=new File(dirFile,srcFile.getName());
copyFile(srcFile,newFile);
}
}
private static void copyFile(File srcFile, File dirFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(dirFile));
byte[] bys=new byte[1024];
int len;
while((len=bis.read(bys))!=-1){
bos.write(bys,0,len);
}
bis.close();
bos.close();
}
}

最后

以上就是火星上纸飞机最近收集整理的关于JavaIO流复制多级文件夹(递归)的全部内容,更多相关JavaIO流复制多级文件夹(递归)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(115)

评论列表共有 0 条评论

立即
投稿
返回
顶部