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
posted
0
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
posted
0
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
posted
0
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.
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
posted
0
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
posted
0
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
posted
0
Manny Bonds: System.gv() does not ensure that Garbage Collector runs! It is just a hint you give to the JVM.