• Post Reply Bookmark Topic Watch Topic
  • New Topic

When I read the file, How can I delete the file

 
Ravi Pydi
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I can read the file using FileInputStream() method, I can able to read the file,but after reading the file I want delete it form the directory, How Can I do it?
Appriciate your help
Gangadhar
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ravi,
Well assuming you opened the stream with new FileInputStream(File file), then all you need to do is call file.delete(). The delete() method is defined in the java.io.File class.
Hope this helps,
Michael Morris
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also make sure you close() the FileInputStream before you try to delete it; some systems may prevent you from deleting if the file is still being read.
And remember to check the return value of delete() to make sure it works. I often do something like this:

This way if something goes wrong you are sure to be notified; otherwise the file might not be deleted, but you don't notice the problem until much later.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!