Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to delete content from a text file?

 
rick collette
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I find there are only read and write methods for accessing a text or binary file. After I write data to a file, is there any way to delete everything from that? I mean how to get the file to its original empty state? Thanks a lot.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To delete a file completely, use File.delete(). The file will no longer exist.
To create a file which exists but has no content (a zero-length file), you can use
new FileOutputStream("filename.txt").close();
(This overwrites any previous file.)
To delete some but not all of the content in a text file, you would probably have to read from the original file, and write to a new file, being careful to only write the parts you want in the new file. Then when done, delete the original file, and rename the new file to the old file name.
 
rick collette
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, Jim. I really appreciate it.

Originally posted by Jim Yingst:
To delete a file completely, use File.delete(). The file will no longer exist.
To create a file which exists but has no content (a zero-length file), you can use
new FileOutputStream("filename.txt").close();
(This overwrites any previous file.)
To delete some but not all of the content in a text file, you would probably have to read from the original file, and write to a new file, being careful to only write the parts you want in the new file. Then when done, delete the original file, and rename the new file to the old file name.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic