Ensuring directory deletion has complete

M

Mark

I am writing a program that adds and removes files cleaning up empty
directories when the last file has been removed; it also automatically
creates the directory when a new file needs to be written. During
testing I have discovered that if a directory is removed and then
recreated in quick succession the directory creation sometimes fails.
The following code highlights this error.

import java.io.File;
public class TestMkdirs {
public static void main(String[] args) {
File test = new File("test");
for (int i=0; i<100; i++) {
test.delete();
if (!test.mkdir()) {
if (!test.exists()) {
System.err.println("Could not create " + i);
}
}
}
}
}

The output is:

Could not create 2
Could not create 3
Could not create 4
Could not create 5
Could not create 6
Could not create 7
Could not create 8
Could not create 9
Could not create 10
Could not create 11
Could not create 12
Could not create 13
Could not create 14
Could not create 15
Could not create 16
Could not create 17
Could not create 18
Could not create 19
Could not create 20
Could not create 21
Could not create 22
Could not create 23
Could not create 24
Could not create 25
Could not create 26

So it fails about 25% of the time. I am using suns jvm 1.4.2_10 on
windows xp sp2, I have also disabled the virus guard to check if that
is causing a problem but still get the same issue. I have also put
print statements on the delete method checking that it only failes to
delete when the directory does not exist and this is always the case.

What I really need is something like FileDescriptor.sync() for
directory deletion to ensure it has really happened at the o/s level
before continuing.

Any help would be gratefully appreciated
 
R

Rodrigo Zechin

Wel... if JNI is an option it would be really simple to do so.
I don't know anything better than FileDescriptor.sync(), so if it's not
working I would go in a lower level with native code.

Zechin
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top