• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deleting files created using FileOutputStream  RSS feed

 
Lee Sha
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can anyone tell me how I can delete a file which is created using FileOutputStream.

For Ex.,

FileOutputStream fo = new FileOutputStream("Test.fo",false) ;

The above statement creates a file called "Test.fo". Now I do some processing with it and at the end of it, I would like to delete it. Please suggest.
 
Rob Spoor
Sheriff
Posts: 21094
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new File("Test.fo").delete();
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just make sure you have closed it first.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"it" being the FileOutputStream. This is a common reason why files may not get deleted. Be sure to close the file using a finally block, to be sure it gets closed promptly even if an exception occurs:

It's also important to check the return value of file.delete(). Even though the stream is closed, it's possible the call may fail for another reason, and you don't want to ignore that. I often choose to throw an exception if a problem occurs, to make sure it doesn't get overlooked. You can do something else instead, but don't simply ignore the return value.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!