• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Phasing problem with File.delete()

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Some times file.delete() method is not working.Its not deleting file from folder.

Actually , i am creating one tmp file, writing into tmp file and deleting actual file and rename tmp file to actual name.
In this case , some time delete method is deleting file, some times its not deleting.

Please help me in this.
Thanks in advance,
Mahendra
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sure that the file is not in use and be sure that you close the file on finally block before trying to delete the file.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its always a good idea to see the return value of the delete() method.
If it returns false that means the file was not deleted and so you can take any action after that.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a similar problem in an example case I created. Basically, I create a File object (oFile) associated with a non-existent file (file1.txt). oFile.exists() returns FALSE as it should. I then create a separate FileWriter object (oFileWriter) associated with the same non-existent file (file1.txt). After the FileWriter is constructed, oFile.exists() returns TRUE (again, as expected). I then attempt to delete the file using oFile.delete(), which consistently returns FALSE. However, oFile.isDirectory(), oFile.isFile(), oFile.getPath(), oFile.getName(), and oFile.toString() all return values correct for the existing file. (I manually delete file1.txt between test runs. ) It finally hit me that file1.txt would remain busy until I executed oFileWriter.close(). When I execute oFileWriter.close() before executing oFile.delete(), the deletion succeeds.

So now my question is, IS THERE A WAY TO DETERMINE IF A FILE IS BUSY?
 
I am a man of mystery. Mostly because of this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic