954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how should i append data to file by overwriting the one's already existing?

hello
everybody

I want to overwrite the names which are already existing and to append the new names in a file. can anybody plz help out.
if i use the FileWriter(Fname,boolean true),
it appends the even the ones which are existing, i want to append the new ones only.

regards
vps

vps
Newbie Poster
10 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

You'll have to read the whole file into memory, make your changes, then rewrite it back out to the file, deleting the old one and recreating the new one. To be safe, recreate the file with a temporary name, then delete the old file, then rename the temp file.

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

Or better, use RandomAccessFile and just write the data you want at the place you want it selectively.
Takes some getting used to as you're no longer dealing with Writers but Streams but well worth is.
Typical sequence:
- move the file pointer
- create a buffer of data to write to the file
- write the buffer

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You