• 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

Strange Problem on deleting a File

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

Here is the situation : I read from File A and create File B.Then in a different method I read from File A and File B and created File C. Now I copy File B to a different directory
and try to delete it from the directory it was created in.I also copy File C to a different directory and delete it from the directory it was created in.
The problem I have is quite unusual. The file C does get moved to a different directory and it gets deleted from the parent directory but File B only gets moved to a different direcory and it DOESN'T get deleted from the parent directory. The value of fileB.delete() is always false.I am not able to delete File B from the parent direcory. The code of copying and deleting for File B and File C is exactly same.It works for File C but it does not work for File B. I am exhausted with all the options and hope that this group will help me find a way out.
I wrote the same line of code in a different program and it works just fine.It copies the file and then delete the file from the parent directory so I feel there is nothing wrong with my code.But there must be something weird I am doing which is causing File B to be copied but not deleted from the current directory.
Any insight into this problem will be highly appreciated.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, if what you want to do is move a file to a different directory, without leaving behind a copy in the orignal directory, then it is probably much quicker (both to code and to execute) if you use the File renameTo() method. If for some reason this doesn't work for you, then this is a tricky problem. The delete() method can be very frustrating in that it provides absolutely no feedback about why it fails; it simply returns false. (And often people don't remember to check this, but that's their fault.) Anyway, my guess is that during the copy process, you opened a FileInputStream or FileReader for the input file, and this is still open. The delete() method seems to fail when the file to delete is still open. To fix this, you need to make sure that the close() method is called when you are done with the file, before you attempt to delete. Don't let garbage collection take care of it - it will happen eventually, true, but you need the file closed now for the delete to work. I always try to put the close() in a finally clause so that it will happen even if an error is thrown:

Or:

Do this religiously with all your streams, and hopefully you will not see this sort of mysterious error again.
 
Chris Mathews
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much Jim. I didn't close File B and that was causing the problem.I can't thank you enough. I was struggling with this problem for a few hours..I knew it was something silly but I didn't know what it was.

Thanks again.
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic