/**
* @description:
* @author: yrm
* @create: 2020-08-17 19:54
**/
public class Demo1 {
public static void main(String[] args) {
//创建一个文件
File file = new File("d:\\aaa");
//showDir(file,0);
deleteDir(file);
}
//递归删除文件夹
public static void deleteDir(File file) {
//遍历文件
File[] files = file.listFiles();
if (files != null && files.length > 0) {
for (File file1 : files) {
//如果是文件夹,则递归重新判断
if (file1.isDirectory()) {
deleteDir(file1);
}else {
System.out.println(file1.getAbsolutePath()+"删除结果:"+file1.delete());
}
}
}
//删除目录
System.out.println(file.getAbsolutePath()+"删除结果:"+file.delete());
}
//递归显示文件夹
public static void showDir(File file,int level) {
System.out.println(getSplit(level)+file.getAbsolutePath());
level++;
//遍历文件
File[] files = file.listFiles();
if (files != null && files.length > 0) {
for (File file1 : files) {
//如果是文件夹,则进行递归判断
if (file1.isDirectory()) {
showDir(file1,level);
}else {
System.out.println(getSplit(level)+file1.getAbsolutePath());
}
}
}
}
//按级别进行分割 文件夹层次分明
public static String getSplit(int level) {
//创建一个StringBuilder对象 用来拼接字符串
StringBuilder sb = new StringBuilder("|");
for (int i = 0; i < level; i++) {
sb.append("_______");
}
//返回拼接的字符串
return sb.toString();
}
}
文件夹基本操作
最新推荐文章于 2021-07-15 12:43:00 发布