• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File f.delete() fails

 
Anand Gondhiya
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anand Gondhiya
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20749
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manny Bonds: System.gv() does not ensure that Garbage Collector runs! It is just a hint you give to the JVM.

pascal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic