Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Phasing problem with File.delete()  RSS feed

 
vijay mahendra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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
 
Vijay Jeyam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Martin Barche
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!