In Java/Unix I understand you can't delete a folder with something in it and I can do it without recursion but this simple solution is great but I just can't see where/when this is deleting the actual files? by using the delete() function. I clearly see it using the delete() on the actual folder at the end but I don't see how it is or when it is calling delete() on the files it just gets them and when I feel it should be calling delete() it makes the recursive call???
I know it is something simple I am missing but it is driving me crazy and no where can I find answer any help would be greatly appreciated.
boolean deleteDirectory(File directoryToBeDeleted) {
File[] allContents = directoryToBeDeleted.listFiles();
if (allContents != null) {
for (File file : allContents) {
deleteDirectory(file);
}
}
return directoryToBeDeleted.delete();
}
CodePudding user response:
directoryToBeDeleted
is not always a directory. If allContents == null
then directoryToBeDeleted
is a file, not a directory as .listFiles()
returns null if the object of type File
is a file and not a folder.