Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with File.delete() method  RSS feed

 
vinay varanasi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a problem with deleting CSV files from my system using the Delete method of File Class.

The problem is that while some times the files get deleted some times it fails.

I have to clean up a certain directory after copying the files to a new loaction.

for some CSV files this works fine while it fails for certain files especially if it is a 0 KB file

Is there any way to force a delete using java other than File.delete(); method. I have also tried File.deleteOnExit() method but it was of no help.
Thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some "operating systems" -- and here I'm thinking of Windows -- don't allow files to be deleted if they are opened by any process. File.delete() will return false in this case. There's nothing you can do except figure out what process is holding the files open and make sure they get closed.
 
vinay varanasi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
I ran the code in debugger mode and foud out that one of the outputstreams was redundant and it was holding up the file from deletion.

I have also come accross apache's class "FileDeleteStrategy" which provides the facility of forced delete. It is contained in the org.apache.commons.io package.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!