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

Recommended Answers

All 2 Replies

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.