simple (curious) question about file deleting

C

contrex

I have a simple question about deleting a file:
Why it isn't deleted immediatly?
There are neither other processes nor threads which access the file
nor any security issues!
Here is my test code:

public void deleteFile(File f) {

if (f!=null && f.exists()) {
f.delete();
System.out.println("DELETED!!");
}

for(int x=0; x<A_BIGNUMBER && f!=null && f.exists(); x++) {
f.delete();
System.out.println("DELETE NR." + x);
}
}

And the output varies. Sometimes is it:

DELETED!!

Sometimes is it:

DELETED!!
DELETE NR.0
DELETE NR.1
DELETE NR.2
DELETE NR.3
....
DELETE NR.464

Yes! 464 tries in order to delete a file!!

So I thought I have to run the garbage collector if the file persists
and I wrote:

public static void deleteFile(File f) {

if (f!=null && f.exists()) {
f.delete();
System.out.println("DELETED!!");
}
if (f!=null && f.exists()) {
System.gc();
f.delete();
System.out.println("SECOND DELETED!!");
}
}

And the file is really deleted at last at the second try (I hope so, I
tried it only some times).
Is this "good design"?
How do you delete a file correctly immediatly?

Thanks in advance!
 
S

Steve W. Jackson

:I have a simple question about deleting a file:
:Why it isn't deleted immediatly?
:There are neither other processes nor threads which access the file
:nor any security issues!
:Here is my test code:
:
:public void deleteFile(File f) {
:
: if (f!=null && f.exists()) {
: f.delete();
: System.out.println("DELETED!!");
: }
:
: for(int x=0; x<A_BIGNUMBER && f!=null && f.exists(); x++) {
: f.delete();
: System.out.println("DELETE NR." + x);
: }
:}
:
:And the output varies. Sometimes is it:
:
:DELETED!!
:
:Sometimes is it:
:
:DELETED!!
:DELETE NR.0
:DELETE NR.1
:DELETE NR.2
:DELETE NR.3
:...
:DELETE NR.464
:
:Yes! 464 tries in order to delete a file!!
:
:So I thought I have to run the garbage collector if the file persists
:and I wrote:
:
:public static void deleteFile(File f) {
:
: if (f!=null && f.exists()) {
: f.delete();
: System.out.println("DELETED!!");
: }
: if (f!=null && f.exists()) {
: System.gc();
: f.delete();
: System.out.println("SECOND DELETED!!");
: }
:}
:
:And the file is really deleted at last at the second try (I hope so, I
:tried it only some times).
:Is this "good design"?
:How do you delete a file correctly immediatly?
:
:Thanks in advance!

Just FYI, there's no need for many of those checks for non-null values
of your "f" variable.

But as to why this happens, it's probably due to the underlying
filesystem and/or disk drive. I just did a small test on my Mac which
shows that only one call to delete() did indeed delete the file. A
second check for exists() was false. It's probably due to some form of
buffering that determines when the file really gets deleted.

= Steve =
 
A

ak

you can easy check if deleting was successfull:
if( f.delete()) {
System.out.println("DELETED");
}
else {
System.out.println("can't delete file");
}

before deleting file ensure that all streams on it was closed
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top