No matter what, it won't delete

Seems File#delete() on Java 6 kinda fails (or not robust). Many people experienced this. Suggestions over internet that I have tried,

  • Call System.gc() before deletion
  • Close all stream, buffered, or what
  • check if it’s exists, writeable, and delete() return true. Unfortunately they’re all true.

do this:

new FileOutputStream(theFileToDelete).close();

and it will throw an exception describing the problem.

Please note that if it works fine, the file is truncated (length = zero), which shouldn’t be a problem, as you were about to delete it anyway.

Nope, nothing thrown.

I bet you’re trying to delete something in the /bin folder in an Eclipse project, and Eclipse quickly scurries off and copies the corresponding file back from /src. Well, maybe. Otherwise I’ve found deletion to work perfectly fine since the dawn of time.

Cas :slight_smile:

You lose the bet :slight_smile: I tried to delete files inside a directory at user.home. The directory is created by this same program. Since I use System#getProperty, I believe I’m using absolute path.

My trolling mind tells me to go for native dll library just to delete.

In windows? If you created it in user.home, the indexer might have it open.

Can you delete the file manually?

delete shouldnt return true in any case in which it isnt actually deleted

maybe you recreate the file yourself automatically ? ;D

Flawlessly, straight to recycle bin :wink:

;D I’ll whack my program hardly if it creates new one right after I call my delete method. “I never taught you that!”.

I haven’t tried Java 7 yet, but some folks out there say it’s fixed with it. Oh yeah this also only happens on Win 7 as far I read.

Some guy online posted this and apparently it works:

That code is just voodoo nonsense.

Cas :slight_smile:

Have you tried writing a small test case that only deletes that file?

As an aside to the issue: it sounds like you could be creating a temp file, if so…why not use the API for that.

Not yet, too lazy.

You mean File#createTempFile? it adds some magic numbers right after my file’s name :cranky:

…and coupled with deleteOnExit and proper termination?

I haven’t tried deleteOnExit yet but it maybe work. Proper termination? I always use System.exit(0) with some of my closing methods (saving data, release everything that has close(), dispose(), cleanup() etc).

Does it work to delete a file in another folder such as “C:\ReBirthTest”?

Yes. Does this have something to do with administrator thing?

Do this. Otherwise you’re just wasting everybodies time. :persecutioncomplex:

Aye captain!

Holy nut! they did! :o