找到一个很好的代码来删除包含子目录的目录,但是可以实现一个(子)目录,例如存在(.data)没有被删除吗?
感谢您的帮助。
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();
}
答案 0 :(得分:0)
也许你可以试试这个:
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
if(children.equals("data")){
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
}
return dir.delete();
}
答案 1 :(得分:0)
试试这个
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
if(children[i].equals("data")||children[i].equals(".data"))
continue;
else
if (!deleteDir(new File(dir, children[i])))
return false;
}
}
return dir.delete();
}