1 Public Static voidDeldir (File f) {2 //determine if it is a directory, not skip it, delete it directly, or, if it is a directory, clear its contents first.3 if(F.isdirectory ()) {4 //get sub File/directory5file[] Subfiles =f.listfiles ();6 //traverse the directory7 for(File subfile:subfiles) {8 //recursive call to delete the file: If this is an empty directory or file, it can be deleted once recursively. If this is a non-empty directory, multiple9 //recursively empties its contents before deletingTen Deldir (subfile); One } A } - //Delete an empty directory or file - F.delete (); the}
By the way, a 64-bit system can probably call tens of thousands of recursion. We don't basically create directories with more than 10,000 layers. So this method can deal with the general situation, without causing problems such as memory overflow.
A way for Java to recursively delete a directory