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?
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?