• 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

Any known bugs w/ File.delete() ?

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my code:
if(fileToDelete != null)
{
boolean deleted = fileToDelete.delete();
System.out.println(deleted);
}
It always returns false. The file does exist, my .java.policy file does allow deleting of files. Yet the file does not get deleted.
Any clue as to what this File newbie could be missing?
 
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
of what I can tell you.. I have not run into such a problem before. It is only when I am trying to delete a file which is locked with me holding its outputStream, I get the false problem. I also encounter an AccessDenied Exception.
Tell me if you are running the code segment on an Applet.
lupo
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most common problem is what gautham said - if there's an InputStream or OutputStream attached to the file which has not been closed, the delete() may fail. Make sure that you close() any attached streams first. (Use the a finally{} clause to ensure this happens even if exceptions are thrown.) Another possible reason for delete() to fail is that the file may not exist (under the name you've given). Try:
System.out.println(fileToDelete.getAbsolutePath());
System.out.println("fileToDelete.exists() ? "exists" : "doesn't exist"));
for more info. (The absolute path tells you where the system is really looking for the file - sometimes it's not where you think.)
 
Bill White
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey what do you know! Thanks guys
 
If you want to look young and thin, hang around old, fat people. Or 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