• Post Reply Bookmark Topic Watch Topic
  • New Topic

Any known bugs w/ File.delete() ?  RSS feed

 
Bill White
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • 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?
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • 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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hey what do you know! Thanks guys
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!