跳过删除循环中存在的目录

时间:2014-12-26 12:06:32

标签: android directory subdirectory

找到一个很好的代码来删除包含子目录的目录,但是可以实现一个(子)目录,例如存在(.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();
}

2 个答案:

答案 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();
}