import java.io.File;
public class DelDir {
public static void main(String[] args) {
delDir(new File("/home/huanyu/Desktop/a")); //如果文件里还有内容,则递归删除
}
public static boolean delDir(File dir){
if (dir.isDirectory()){
String[] children = dir.list();
for (int i=0; i<children.length;i++) {
boolean success = delDir(new File(dir, children[i]));
if (!success)
return false;
}
}
if (dir.delete()){
System.out.println("目录已删除!!!");
return true;
}else {
System.out.println("目录删除失败!!!!!");
}
return false;
}
}
![img_5cb1628dc1749482d6cf78c0d50b8b00.png](https://yqfile.alicdn.com/img_5cb1628dc1749482d6cf78c0d50b8b00.png?x-oss-process=image/resize,w_1400/format,webp)
![img_2ff2143d381b280a6b59af73b59a33cb.png](https://yqfile.alicdn.com/img_2ff2143d381b280a6b59af73b59a33cb.png?x-oss-process=image/resize,w_1400/format,webp)