用递归方法删除不为空的目录

来源:互联网 发布:倾斜摄影测量软件 编辑:程序博客网 时间:2024/05/18 16:54

If the directory is not empty, it is necessary to first recursivelydelete all files and subdirectories in the directory. Here is amethod that will delete a non-empty directory.

    // Deletes all files and subdirectories under dir.
// Returns true if all deletions were successful.
// If a deletion fails, the method stops attempting to delete and returns false.
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;
}
}
}

// The directory is now empty so delete it
return dir.delete();
}
原创粉丝点击