解决方案
下面的示例演示了如何使用 File 类的 dir.isDirectory()、dir.list() 和 deleteDir() 方法在删除文件和目录后删除目录。
import java.io.File;
public class Main {
public static void main(String[] argv) throws Exception {
deleteDir(new File("c:\\temp"));
}
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir (new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
System.out.println("The directory is deleted.");
}
}