把文件目录结构想成一棵树,用深度优先遍历算法逐个去删除树的叶子
package test;
import java.io.File;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f=new File("E:/新建文件夹/");
deleteFile(f);
}
public static void deleteFile(File file)
{
if(file.isFile()) {
file.delete();
return;
}
File[] fs=file.listFiles();
for (File f:fs)
{
if (f.isFile())
{
f.delete();
}
else{
deleteFile(f);
f.delete();
}
}
file.delete();
}
}