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 Filecontent delete Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Filecontent delete" Watch "Filecontent delete" New topic
Author

Filecontent delete

sreejith panampilly
Ranch Hand

Joined: Jun 15, 2006
Posts: 49
How can delete some content of a file without writing to that file
Ådne Brunborg
Ranch Hand

Joined: Aug 05, 2005
Posts: 208
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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Jim Yingst
Wanderer
Sheriff

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


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Filecontent delete