File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes how to delete content from a text file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to delete content from a text file?" Watch "how to delete content from a text file?" New topic
Author

how to delete content from a text file?

rick collette
Ranch Hand

Joined: Mar 22, 2002
Posts: 208
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
rick collette
Ranch Hand

Joined: Mar 22, 2002
Posts: 208
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.
 
 
subject: how to delete content from a text file?