Is there a way to delete a directory with files in it?
I am working on a program right now, and i need to know if there is a way to delete a directory that has files in it?
or do i have to write another program that gets the list and deletes each file individually?if this is the solution, how do you do this?
Right now, i would prefer some of the logic and some "code snippets" but please do not write the entire code out for me.
thank you.
(snippets include how to poll, and how to call the polled item so i can delete it...)
[504 byte] By [
pros599a] at [2007-11-26 12:22:15]

# 1
you'll be needing this.File - http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html=)null
# 2
See this tried-and-true 3rd-party open source (free) library: http://jakarta.apache.org/commons/io/The FileUtils class in it is what you're after.
# 3
> Right now, i would prefer some of the logic and some "code snippets" but
> please do not write the entire code out for me.
So I assume this is some sort of "homework" (maybe self imposed).
You can't use File's delete() to delete a non empty directory as I guess you
have discovered. So basically, yes, you have delete each child file
separately and then delete the directory.
But it gets worse. The directory may itself contain directories and these may
not be empty...
There's a fairly extensive snippet here:
http://www.exampledepot.com/egs/java.io/TraverseTree.html which shows
how you can "visit" every file and folder inside a given folder (children,
grandchildren etc) and "process" them. It may help - but be warned there's
a nearby example which more or less solves your problem.
# 4
> > Right now, i would prefer some of the logic and
> some "code snippets" but
> > please do not write the entire code out for me.
>
> So I assume this is some sort of "homework" (maybe
> self imposed).
>
> You can't use File's delete() to delete a non empty
> directory as I guess you
> have discovered. So basically, yes, you have delete
> each child file
> separately and then delete the directory.
>
> But it gets worse. The directory may itself contain
> directories and these may
> not be empty...
>
> There's a fairly extensive snippet here:
> http://www.exampledepot.com/egs/java.io/TraverseTree.h
> tml which shows
> how you can "visit" every file and folder inside a
> given folder (children,
> grandchildren etc) and "process" them. It may help -
> but be warned there's
> a nearby example which more or less solves your
> problem.
Um, did my post above get covered with lemon juice or otherwise become invisible?
Jakarta Commons IO FileUtils already does the job.
# 5
If it is indeed "homework", whether self-imposed or not, then looking at the source for FileUtils may be too confusing. A more concise example isn't necessarily a bad thing.
# 6
> Um, did my post above get covered with lemon juice or otherwise become
> invisible? Jakarta Commons IO FileUtils already does the job.
Sorry - I just thought it might be too thorough and complete if the OP was
starting to write recursive methods. It (the whole class) is useful and
worth study.