共计 678 个字符,预计需要花费 2 分钟才能阅读完成。
// 文件删除测试
import java.io.*;
public class Delete {
public static void main(String[] args) throws IOException {
Delete.delete("F:b");
System.out.println("ok");
}
// 文件名或者文件删除(可以删除包含的子文件和文件夹)
public static boolean delete(String s) throws IOException {
File file = new File(s);
if(!file.delete()) {
String[] f = file.list();// 列出当前文件夹中的所以文件包括文件夹
for(int i = 0; i < f.length; i++) {
delete(file.getAbsolutePath() + "" + f[i]);// 对子文件夹进行处理
return delete(file.getAbsolutePath());// 返回上级目录
}
}
return true;
}
}