java在文本文件的中间写入

java在文本文件的中间写入,java,Java,我有一个文本文件,其中的文本如下所示: first line : some content second line: some content third line : some content fourth line: some content 我想在第2行和第3行之间添加“一些内容”,但是 out.println(somecontent) 它写在文件前面。另一点是,我有时需要将第一行内容更改为第三行内容。最好的解决方案是创建第二个文件还是将其临时保

我有一个文本文件,其中的文本如下所示:

    first line : some content
    second line: some content
    third line : some content
    fourth line: some content
我想在第2行和第3行之间添加“一些内容”,但是

    out.println(somecontent)

它写在文件前面。另一点是,我有时需要将第一行内容更改为第三行内容。最好的解决方案是创建第二个文件还是将其临时保存在字符串中?如何让我的程序在其他文件之间写入字符串

如果文件很小,读取并在内存中执行修改,然后写回磁盘;如果它太大,则读取一个数据块,然后使用方法写回新的数据块