如何在Java文本文件中追加现有行 [英] How to append existing line within a java text file
问题描述
我在添加到文本文件中的现有行时遇到问题而没有覆盖该特定行或添加新行。
I'm having trouble adding to an exsisting line in a text file without overwriting that particular line or adding a new line.
例如,我有一行在我的文本文件中:
for example, i have a line in my text file which is:
hello my name is
我想添加到这一行,所以它变为:
I would like to add to this line so it becomes:
hello my name is joe bloggs
谢谢
我有一项创建帮助台程序的任务,我正在尝试合并一项功能,使用户能够编辑他们发布的问题。因此,程序将需要能够在文本文件中追加任何行 - 不一定只是最后一行
推荐答案
如果它不在文件的末尾,那你就麻烦了 - 你基本上是在文件中间讨论插入数据,这在传统上不受支持文件系统。
If it's not at the end of the file, you're in trouble - you're basically talking about inserting data in the middle of a file, which isn't traditionally supported by file systems.
正常的方法是创建一个 new 文件 - 从旧文件复制插入点之前的部分,然后编写新数据,然后复制原始文件的其余部分。最后,做任何你需要的重命名/删除。
The normal way to approach this is to create a new file - copy the portion before the insertion point from the old file, then write your new data, then copy the remainder of the original file afterwards. Finally, do whatever renaming/deleting you need.
这篇关于如何在Java文本文件中追加现有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!