delete non-empty folder?

K

Kaidi

I just wonder is there any "one step" method to delete a folder even
it is not empty?
I know the java.io.File has delete for empty folders. Surely we can
write something recursively to first delete the folder's content
before we delete the folder, but any other way?

Thanks.
 
P

Paul Lutus

Kaidi said:
I just wonder is there any "one step" method to delete a folder even
it is not empty?
I know the java.io.File has delete for empty folders. Surely we can
write something recursively to first delete the folder's content
before we delete the folder, but any other way?

No, AFAIK, there is no other way within Java.
 
X

xarax

Paul Lutus said:
No, AFAIK, there is no other way within Java.

And you have to handle the situation of nested
folders by building your own in-memory representation
of the folder tree structure, then delete from the bottom
to the top using a depth-first tree walk. And you have
to take care that you handle the situation where new
files or folders are created by an asynchronous process
while you're scanning/deleting.

Good luck.
 
F

Frank

xarax said:
And you have to handle the situation of nested
folders by building your own in-memory representation
of the folder tree structure, then delete from the bottom
to the top using a depth-first tree walk.

Why wouldn't a simple post-order traversal work?

E.g.:

void delTree(File dir) {
for each file in dir {
if (file.isDirectory()) {
delTree(file);
}
file.delete();
} // end for
}

-Frank
 
A

Ann

Kaidi said:
I just wonder is there any "one step" method to delete a folder even
it is not empty?
I know the java.io.File has delete for empty folders. Surely we can
write something recursively to first delete the folder's content
before we delete the folder, but any other way?

Thanks.

call a method(), that is only one step
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top