Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

File.deleteDirectory

Thread view: 
Roedy Green - 19 Jan 2006 10:20 GMT
File.delete has a rather onerous condition:

the directory must be empty in order to be deleted.

to delete, you must recursively delete all the files and directories
in order to delete a tree.

It seems to me that a sane OS should do this in the background.  It
could nip the root of the tree and at its leisure reclaim the space of
the occupied files.

Are there sane OS's?  Are there ones that will chase the tree for you?

If so I suggest adding a new method to File:

File.deleteDirectory ( File dir, boolean onlyIfEmpty )

On the other paw, a file might be in use in the tree and can't be
deleted. You don't know that until you traverse the tree. Any thoughts
on speeding this up?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Thomas Weidenfeller - 19 Jan 2006 11:31 GMT
> It seems to me that a sane OS should do this in the background.

It depends on your definition of a sane OS. A slight mistake could kill
a whole file system - if you accidentally try to delete the root directory.

> Are there sane OS's?

Well, I know that in some versions of Unix you can unlink a directory,
leaving a corrupted file system behind. Not what you really want.

> File.deleteDirectory ( File dir, boolean onlyIfEmpty )

/* hasta la vista baby */
File.deleteDirectory(new File("/"), false);

> On the other paw, a file might be in use in the tree and can't be
> deleted.

On Unix you can delete files while they are in use. The OS removes the
file directory entry, but the file contents remains until the last
process using that file has closed it. Then the contents is removed,
too. Very nice for temporary files. You create them, you delete them,
and afterwards you use them :-) If the application exits, crashes, etc.
the temporary file is nicely cleaned up by the OS.

On the other hand, directories in use as mount points can't be removed.
You first have to unmount the file system. A file system driver trying
to clean up such a tree would be in trouble.

> You don't know that until you traverse the tree. Any thoughts
> on speeding this up?

A file system might anyhow have to recursively traverse a directory tree
to find all disk space claimed by the data in the tree. And for each
file and subdirectory found in a directory tree it would have to do some
 housekeeping to reclaim the used disk space. I would guess it can be
done (much?) faster if done inside the OS file system driver, but still
there would be some time consuming disk I/O be involved.

To speed this up, exec a separate process, or a separate thread and do
the deletion in the background. This is basically what you would like to
see from the OS: The directory tree is removed in the background.

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Roedy Green - 19 Jan 2006 11:57 GMT
On Thu, 19 Jan 2006 12:31:47 +0100, Thomas Weidenfeller
<nobody@ericsson.invalid> wrote, quoted or indirectly quoted someone
who said :

>/* hasta la vista baby */
>File.deleteDirectory(new File("/"), false);

You can do the same thing recursively, carrying on if any individual
delete fails.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 19 Jan 2006 17:25 GMT
On Thu, 19 Jan 2006 11:57:06 GMT, Roedy Green
<my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
indirectly quoted someone who said :

>>/* hasta la vista baby */
>>File.deleteDirectory(new File("/"), false);
>
>You can do the same thing recursively, carrying on if any individual
>delete fails.
Yours is an argument for making the garden variety delete insist on
emptiness.  If you are trying to delete the tree, you are more likely
to do undesired damage with a home grown method, especially during
debugging.   Perhaps a safety check would be a provided limit on how
many files or total space you are prepared to delete.


Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Rogan Dawes - 20 Jan 2006 07:15 GMT
> On Thu, 19 Jan 2006 11:57:06 GMT, Roedy Green
> <my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
[quoted text clipped - 11 lines]
>
>  

How difficult is this really?

    /**
     * Recursive delete files.
     */
    public static boolean recursiveDelete(File dir) {
        String[] ls = dir.list();

        for (int i = 0; i < ls.length; i++) {
            File file = new File(dir, ls[i]);
            if (file.isDirectory()) {
                if (!recursiveDelete(file)) return false;
            } else {
                if (!file.delete()) return false;
            }
        }
        return dir.delete();
    }
Luc The Perverse - 19 Jan 2006 21:52 GMT
> File.delete has a rather onerous condition:
>
[quoted text clipped - 16 lines]
> deleted. You don't know that until you traverse the tree. Any thoughts
> on speeding this up?

No - and you provided your own reason in the last paragraph.

As for a utility to do it in the background - this would be easy, just a
simple EXE file and call it from another thread.  Or even make the EXE file
spawn it's own thread and run in the background.

--
LTP

:)
Gordon Beaton - 20 Jan 2006 08:10 GMT
> It seems to me that a sane OS should do this in the background.  It
> could nip the root of the tree and at its leisure reclaim the space of
> the occupied files.
>
> Are there sane OS's? Are there ones that will chase the tree for
> you?

I use a sane OS. The system call will only delete a leaf node in the
file system (i.e. a file or an empty directory), but the OS provides a
command line utility that will do the recursion if I ask it to.

The system call will either do the whole job or nothing at all in case
of failure, while the utility may fail after removing part of the
tree. I wouldn't want a system call that might return after finishing
only part of the job.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Jacob - 20 Jan 2006 08:21 GMT
> File.delete has a rather onerous condition:
>
[quoted text clipped - 16 lines]
> deleted. You don't know that until you traverse the tree. Any thoughts
> on speeding this up?

I guess you need to visit every file to check for permissions
as well as usage etc. And I'd suspect there might be notification
mechanisms at a low level that sends update events in aid of the GUI
and these will have to be synchronous.

But as far as your API suggestion goes I fully agree. If you want to
delete a sub tree you'd have to write the method yourself. And accidently
deleting the entire filesystem will be as easy with this one as with a JDK
method that does the same. (The only difference is that Sun can't be blamed
for it. Perhaps thats the reason they didn't include it :-)

Btw, Python has exactly this feature: shutil.rmtree() which do the
recursion for you.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.