gpt4 book ai didi

java - 在文件开头追加文本而不删除文件

转载 作者:行者123 更新时间:2023-12-02 05:47:48 25 4
gpt4 key购买 nike

有很多关于此的帖子,但我找不到一个可以正常工作的帖子,例如下面的代码可以运行,但它删除了第一行。

我想在文件开头插入一些文本而不删除任何内容,只是插入。这可能吗?

RandomAccessFile f = new RandomAccessFile(new File("myFile.txt"), "rw");
f.seek(0);
f.write("Blah Blah This is first line".getBytes());
f.close();

我也尝试过this ,仍然删除前12个字节

最佳答案

伪代码是

String existing = Read whole content of file. 

String newcontent = "Blah Blah This is first line" + existing

f.write(newcontent);

关于java - 在文件开头追加文本而不删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23886299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com