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