• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

File f.delete() fails

 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic