Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing to same file

Status
Not open for further replies.

lowbk

Technical User
Nov 26, 2001
162
SG
hi, i need some advice/tips.

suppose i need to modify the contents of a file. usually i will read line by line, do the modifications then write to another file.

is it possible if i want the modifications to be done on the SAME file directly, ie input file = output file?

i looked at bufferedwriter, printwriter, filewriter, but no hints of how that can be done..
 
The general approach is to begin writing at the point (position) in the file where the first change occurs and then write the remainder of the file from that position.

This is simple when you can buffer the entire file into memory. Somewhat more difficult if you cannot as you must be sure not to overwrite part of the file that you have not yet read into your memory buffer. Not rocket science in either case.

does that help?

-pete
 
can you give me hints on which java class to use? filewriter seems to overwrite the existing file or append at the end..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top