Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Doesn't File.delete() delete immediately?

Reply
Thread Tools

Doesn't File.delete() delete immediately?

 
 
vandmzah@gmail.com
Guest
Posts: n/a
 
      07-05-2006
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?

 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      07-05-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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?


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

 
Reply With Quote
 
 
 
 
Philipp Leitner
Guest
Posts: n/a
 
      07-05-2006
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

 
Reply With Quote
 
Ben_
Guest
Posts: n/a
 
      07-05-2006
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To delete or not to delete? Lethal Computer Support 15 07-24-2005 11:47 PM
Can someone tell me why I can't delete this file? and why it blue screens WinXP Pro on delete? zZz Computer Support 1 01-12-2005 02:37 AM
Cannot Delete, (The Delete Key Won't Work) Lee Something Computer Support 13 10-15-2003 09:51 PM
Overloading new[] and delete[]: how do they vary from new and delete? HeroOfSpielburg C++ 1 08-06-2003 03:58 AM
delete on delete ! Sandeep Grover C++ 19 07-22-2003 10:09 AM



Advertisments