当前位置:网站首页>8.递归遍历和删除案例
8.递归遍历和删除案例
2022-08-09 09:23:00 【过来我的小熊】
递归遍历和删除文件夹案例
package com.io.file;
import java.io.File;
/**
* 递归遍历和删除
*/
public class Demo3 {
public static void main(String[] args) {
listDir(new File("E:\\桌面\\aa"));
deleteDir(new File("E:\\桌面\\aa"));
}
// 递归遍历文件夹
public static void listDir(File dir){
File[] files = dir.listFiles();
System.out.println(dir.getAbsolutePath());
if (files!=null&&files.length>0){
for (File file : files) {
if (file.isDirectory()){
listDir(file); // 递归
}else {
System.out.println(file.getAbsolutePath());
}
}
}
}
// 递归删除文件夹
public static void deleteDir(File dir){
File[] files = dir.listFiles();
if (files!=null && files.length>0){
for (File file : files) {
if (file.isDirectory()){
deleteDir(file); // 递归
}else {
// 删除里层文件
System.out.println(file.getAbsolutePath() + "删除" + file.delete());
}
}
}
// 删除最外层的文件夹 dir
System.out.println(dir.getAbsolutePath() + "删除" + dir.delete());
}
}
边栏推荐
- Anti App so层对抗分析
- A first look at the code to start, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, the first time to run the golang program EP01
- GBase数据库中,源为 oracle 报出“ORA-01000:超出打开游标最大数”
- These 12 GIS software are better than the other
- 一篇文章让你彻底搞懂关于性能测试常见术语的定义
- 关于链表的几道大厂面试题
- Ovie map computer terminal and mobile terminal can not be used, is there any alternative map tool
- Ontology Development Diary 01-Jena Configuration Environment Variables
- 第四讲 SVN
- on duplicate key update
猜你喜欢
命令行查询数据库
性能测试报告包括哪些内容?模板范文哪里找?看这里
Sweet alert
How much do you know about the mobile APP testing process specifications and methods?
学习栈的心得和总结(数组实现)
可以写进简历的软件测试项目实战经验(包含电商、银行、app等)
"The camera can't be used" + win8.1 + DELL + external camera + USB drive-free solution
【个人学习总结】CRC校验原理及实现
自动化测试简历编写应该注意哪方面?有哪些技巧?
黑盒测试常见错误类型说明及解决方法有哪些?
随机推荐
黑盒测试常见错误类型说明及解决方法有哪些?
These 12 GIS software are better than the other
TestNG使用教程详解
本体开发日记05-努力理解SWRL(中)
Do you know the basic process and use case design method of interface testing?
Summary of steps and methods for installing and uninstalling test cases that you must read
本体开发日记05-努力理解SWRL(Usage Suggestions)
第四讲 SVN
jfinal加载配置文件原理
命令行查询数据库
使用Protege4和CO-ODE工具构建OWL本体的实用指南-1.3版本(4.Building An OWL Ontology)
软件测试面试题目:请你列举几个物品的测试方法怎么说?
"The camera can't be used" + win8.1 + DELL + external camera + USB drive-free solution
使用Protege4和CO-ODE工具构建OWL本体的实用指南-1.3版本(7.4 Annotation Properties-注释属性)
米斗APP逆向分析
有返回值的函数
字典
web测试之功能测试常用的方法有哪几种?有什么要点要注意?
JS-常用方法整理
接口设计