aspose file tools*
The moose likes Java in General and the fly likes File   f.delete()   fails Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "File   f.delete()   fails" Watch "File   f.delete()   fails" New topic
Author

File f.delete() fails

Anand Gondhiya
Ranch Hand

Joined: Feb 24, 2004
Posts: 155
Hello Everybody ,

I have following code:

File f = new File("abc.csv");
boolean success= f.delete();

value of success is full after delete attempt. above statements are there in my code and success is "false". But when I try to run above two statements in test.java , it is able to delete the file.

i.e the question is , why f.delete() fails sometimes and succeeds sometimes ?

Thanks for your help.
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
the file is opened for R/W? the file can not be deleted because of user rights ? the files does not exist ?

pascal
Abhijeet Thacker
Greenhorn

Joined: Jan 15, 2005
Posts: 16
Hi Anand,
Check out following things.

1. To delete a file you must check if file exists in first place. So before you fire delete() make sure that file exists

2. Once you make sure that file exists and if delete fails then it means that there is some handle open for the file. Please close all the handles for that particular file and then fire the delete command.

3. make sure that file in question is not being used by some other program. Even in this case delete() will return false.

Please perform above mentioned steps in order and I am sure that delete() will succedd 99.99% of the time.

HTH


Always code as if person who is going to maintain your code is a maniac serial killer and knows where you live
Anand Gondhiya
Ranch Hand

Joined: Feb 24, 2004
Posts: 155
Thanks for your help. Let me put the real code .
1. To delete a file you must check if file exists in first place. So before you fire delete() make sure that file exists

f1.exists() returns true
2. Once you make sure that file exists and if delete fails then it means that there is some handle open for the file. Please close all the handles for that particular file and then fire the delete command.

I have closed the handlers , see the code below ( src.close() ;
3. make sure that file in question is not being used by some other program. Even in this case delete() will return false.


I am sure that no other Application is using this file.



Please comment on this.

thanks.
-Anand.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

It isn't obvious to me that closing a channel automatically closes the input stream it was derived from, and the documentation doesn't say it does. Your code is an experiment that apparently proves it doesn't. Try closing the FileInputStream.
Manny Bonds
Greenhorn

Joined: Aug 04, 2006
Posts: 4
Quick suggestion. If you are running the file deltion code on a windows machine execute call for garbage collection System.gc() before you delete the file. This ensures under windows that all resources including file resoruces are released.

This may be an expensive call depending on the speed of your processor or heap size. You may want to pool all file deletions and run once when system is in down time.

***Not necessary under unix machines
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
I agree with Paul.

You need to close the InputStream that contains the Channel.

This will also close the Channel.

The reverse is not true.

Guy
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
Manny Bonds: System.gv() does not ensure that Garbage Collector runs! It is just a hint you give to the JVM.

pascal
 
jQuery in Action, 2nd edition
 
subject: File f.delete() fails
 
Similar Threads
How can we trust security
delete()
Delete a file in java
File Deletion is failing.
Unable to delete file