Doesn't File.delete() delete immediately?

V

vandmzah

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.delete();
if(!deleted)
System.out.println("Can't delete" + leftDirs);
}
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 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?
 
O

Oliver Wong

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.delete();
if(!deleted)
System.out.println("Can't delete" + leftDirs);
}
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 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
 
P

Philipp Leitner

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
 
B

Ben_

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top