how to write content in a Specific position in a File

前端 未结 5 1159
北海茫月
北海茫月 2020-11-29 10:07

Suppose I have a file named abhishek.txt and that contains the following line

I am , and what is your name.

Now I want to write

5条回答
  •  借酒劲吻你
    2020-11-29 10:45

    You have three choices:

    1. Pad the original entry with enough space that RandomAccessFile can be used (and you don't care about some extra trailing space. Easy, but limited utility
    2. Use a templating engine (StringTemplate, Velocity, etc) if you are doing this a lot. Possibly too much work if you are doing this just once.
    3. Write your own simple templating engine that reads it into memory does a regex replacement and writes it back. Not especially efficient but if you have very simple and limited usage, maybe the best compromise.

提交回复
热议问题