How can delete some content of a file without writing to that file
Joined: Aug 05, 2005
Originally posted by sreejith panampilly: How can delete some content of a file without writing to that file
There are four operations you can do on a file - Create, Read, Update, Delete (CRUD). What you want to do is Update the file, and you cannot do that without writing to it.
Entia non sunt multiplicanda praeter necessitatem
Joined: Jan 29, 2003
About all you can do is read the whole file, write the parts you like back out to a new file and skip the parts you want to delete. At this point I always drag this out of my rusty old toolbox:
read original write temp rename original to backup rename temp to original erase backup
That always has a good copy of the original and/or new file in case you kick the plug out of your PC in mid-process.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Jan 30, 2000
If you want to delete content from the end of a file, you do have a couple of faster options. You can use RandomAccessFile's setLength() method, or FileChannel's truncate() method, to change the file length to something smaller. This effectively deletes the bytes at the end. If you want to delete content from the beginning or middle, though - for that you really want to do as Stan suggested.