当前位置:网站首页>8. Recursively traverse and delete cases

8. Recursively traverse and delete cases

2022-08-09 09:33:00 come here my bear

Recursively traverse and delete folders case

package com.io.file;import java.io.File;/*** recursively traverse and delete*/public class Demo3 {public static void main(String[] args) {listDir(new File("E:\\Desktop\\aa"));deleteDir(new File("E:\\Desktop\\aa"));}// recursively traverse the folderpublic 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); // recurse}else {System.out.println(file.getAbsolutePath());}}}}// recursively delete folderspublic static void deleteDir(File dir){File[] files = dir.listFiles();if (files!=null && files.length>0){for (File file : files) {if (file.isDirectory()){deleteDir(file); // recursive}else {// delete inner fileSystem.out.println(file.getAbsolutePath() + "delete" + file.delete());}}}// delete the outermost folder dirSystem.out.println(dir.getAbsolutePath() + "delete" + dir.delete());}}
原网站

版权声明
本文为[come here my bear]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208090923102104.html