Type: Tertiary directory A (Dir) – B (Dir) – C (File)
①File child = new File(children[i]);
File child = new File(dir, children[I]); File child = new File(dir, children[I]);
Passing in the parent directory correctly identifies the child’s type.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Demo: Deletes only files, not folders
public boolean deleteDir(File dir) {
if (dir == null) {
return false;
}
if (dir.isDirectory()) {
String[] children = dir.list();
int len = children.length;
if (len == 0) {
return true;
}
for (int i = 0; i < len; i++) {
File child = new File(dir, children[i]);
boolean success;
if (child.isDirectory()) {
if (child.list().length == 0) {
return true;
}
success = deleteDir(child);
} else {
success = child.delete();
}
if(! success) {return false;
}
if (i == len - 1) {
return true; }}}return dir.isDirectory();
}
Copy the code
@ See 👉 github.com/javakam/Fil…