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 / July 2006

Tip: Looking for answers? Try searching our database.

Doesn't File.delete() delete immediately?

Thread view: 
vandmzah@gmail.com - 05 Jul 2006 07:00 GMT
Hi,

I am discovering that File.delete() does not work the way I expect. I
am not sure if what I observe is expected behavior or if not where to
look for a problem.In my code I have a method to delete contained empty
directories in tempDir. It looks pretty much like this:

protected void clear() {
      File[] leftDirs = tempDir.listFiles();
       for (int i = 0; i < leftDirs.length; i++) {
           boolean deleted=leftDirs[i].delete();
           if(!deleted)
               System.out.println("Can't delete" + leftDirs[i]);
       }
       leftDirs = tempDir.listFiles(); //Try to get the
//    list again
if(leftDirs.length>0)
           System.out.println("Files not deleted!" );
}
I am getting from time to time the message "Files not deleted!" without
getting the "Can't delete" + leftDirs[i] message. (I'm running
j2re1.4.2_08 on XP)

In my test case I am trying to delete a single subdirectory
directory(in context of more complex application). When I set a
breakpoint at the second 'if' statement, the directory is never
there.

It looks like the delete() method returns true, and the directory is
being deleted, but not immediately. My expectation was that if delete()
returns true, it is guaranteed that the directory is no longer there. I
do not want to continue without being sure that the directory is
deleted.

Could please someone explain?
Oliver Wong - 05 Jul 2006 15:54 GMT
> Hi,
>
[quoted text clipped - 31 lines]
>
> Could please someone explain?

   I don't see anything in the JavaDocs to deny your assumptions. Did you
check if this also occurs in 1.5? If so, perhaps you should file a bug with
Sun.

   - Oliver
Philipp Leitner - 05 Jul 2006 17:43 GMT
I admit it's a little far-fetched, but are you sure there isn't some
other application/process/thread writing files into this directory
between your 2 calls to File.list() ? What is the output if you don't
just write your error message, but the list of file names that the
second File.list() returns?

/philipp
Ben_ - 05 Jul 2006 19:25 GMT
Hello,

Actually, it would not surprise me that much, especially when you tell its
intermittent.

I don't know for sure, but I can easily imagine the OS returns true on
delete() to kind of tell "it accepts the request for deleting the file", but
does not do it immediatly for some reason (file locked by the virus
scanner -- especially e.g. a large zip file, operations are buffered and
delayed, etc).

I would advise to search the Bug Database (http://bugs.sun.com/bugdatabase/)
for known bugs (and unexpected behaviors :-) ?

For example, I see bug ID 4045014 "In Win95 JDK 1.1.1, File.delete() fails
to delete the file from disk" closed as "not a bug".

I didn't look further, but I would expect that looking at more stuff like
that will tell you that file system operations are not working as one might
expect in the first place.


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.