Question:
In java how do you delete the contents of a directory with out deleting the directory itself?
Simon
2009-07-13 04:10:39 UTC
Hi, I would like to know how in java you can delete all the files in a directory but keep the directory?

Many thanks
Three answers:
husoski
2009-07-13 14:16:20 UTC
Oddly, I recently had to write a script to do just this because Windows doesn't have a command that will do this. I was working from the CMD shell, not using Java, because--sadly--there's no JDK installed most places I go.



The logic is the same, though. Loop through each file in the directory and delete it. If the file file is subdirectory, recursively call self on that subdirectory to delete it.



There's a simple example of this on The Java Developer's Amanac web page:



http://www.exampledepot.com/egs/java.io/DeleteDir.html
anonymous
2009-07-13 13:23:28 UTC
You can get a list of all the files in a directory and loop through them one at a time. Thats how I do it...
nontarzaniccaulkhead
2009-07-13 11:23:51 UTC
Probably in exactly the same way that you'd do so in any other country in the world.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...