how to write content in a Specific position in a File

前端 未结 5 1154
北海茫月
北海茫月 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:56

    I managed to do it using RandomAccessFile:

    With this, you get a file containing exactly the expected 'I am Abhishek' content. This works even if you would had content after the first line, which you want to keep in the file(this was my initial problem: I had a large file and I had to insert some content after a certain String) rather than write at the end of the file, which is easier.

提交回复
热议问题