The only way to do this is to read the file into memory, modify the internal representation of the data, and write it back out.
There are some variations on this process, but in short; files are not like real paper and ink documents: You can't insert content into the middle of a file, only replace what is already there.
Edit: To clarify, you can only grow a file at the end; not in the middle.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…