JAVA中的File类中有一个delete()方法。
但是这个方法只能删除"文件"和"一级目录",删除多级目录怎么做?
让我们使用下这个递归函数来实现。
public static void main(String[] args) throws IOException { File file = new File("aaa"); deleteDir(file); } private static void deleteDir(File files) { if (files.exists()) { File[] fileList = files.listFiles(); for (File file : fileList) { if (file.isFile()) { file.delete(); } else { deleteDir(file); } } files.delete(); } }
10-01
219
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountBlack.png)
07-26
1099
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountBlack.png)
02-11
1836
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountBlack.png)
09-01
748
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountBlack.png)