Java 实例 - 删除目录
Java 教程
· 2019-02-11 05:58:08
以下实例演示了使用 File 类的 ofdir.isDirectory(), dir.list() 和 deleteDir() 方法在一个个删除文件后删除目录 :
Main.java 文件
import java.io.File;
public class Main {
public static void main(String[] argv) throws Exception {
// 删除当前目录下的 test 目录
deleteDir(new File("./test"));
}
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;
}
}
}
if(dir.delete()) {
System.out.println("目录已被删除!");
return true;
} else {
System.out.println("目录删除失败!");
return false;
}
}
}
以上代码运行输出结果为:
目录已被删除!
点击查看所有 Java 教程 文章: https://codercto.com/courses/l/12.html
Data Structures and Algorithm Analysis in Java
Mark A. Weiss / Pearson / 2006-3-3 / USD 143.00
As the speed and power of computers increases, so does the need for effective programming and algorithm analysis. By approaching these skills in tandem, Mark Allen Weiss teaches readers to develop wel......一起来看看 《Data Structures and Algorithm Analysis in Java》 这本书的介绍吧!
RGB转16进制工具
RGB HEX 互转工具
SHA 加密
SHA 加密工具