• 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

how to delete content from a text file?

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic