如何在文件的特定位置写入内容

发布于 2024-11-03 05:29:07 字数 192 浏览 5 评论 0原文

假设我有一个名为 abhishek.txt 的文件,其中包含以下行

我是,你叫什么名字。

现在我想写

阿布舍克

在“我是”之后就像我是阿布舍克一样,.. 如何直接把这个具体位置的内容写出来。

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

Abhishek

after "I am" like I am Abhishek, ..
How to write the content in this specific position directly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回眸一遍 2024-11-10 05:29:07

您无法将数据插入文件...文件系统(通常)根本不支持此类操作。通常,您会打开一个文件进行读取,另一个进行写入,将文件的第一部分从一个流复制到另一个流,写入额外的部分,然后复制文件的第二部分。

如果您尝试替换原始文件,则需要将其删除并将新文件移动到位。

有时,一次性将整个文件读入内存可能会更简单 - 这取决于您想要执行的操作以及文件有多大。

You can't insert data into a file... file systems (in general) simply don't support such an operation. Typically you'd open one file for reading and another for writing, copy the first part of the file from one stream to the other, write the extra part, then copy the second part of the file.

If you're trying to replace the original file, you'd then need to delete it and move the new file into place.

Sometimes it may be simpler to read the whole file into memory in one go - it depends on exactly what you're trying to do, and how big the file is.

↘紸啶 2024-11-10 05:29:07

您无法将数据插入文件中。您可以使用 RandomAccessFile 覆盖特定位置的数据。然而,插入需要更改其后的所有数据。对于你的情况,尝试这样的事情:

File file = new File("abhishek.txt");
Scanner scanner = new Scanner(file).useDelimiter("\n");
String line = scanner.next();
String newLine = line.substring(0, 5) + "Abhishek" + line.substring(5);
FileWriter writer = new FileWriter(file);
writer.write(newLine);
writer.close();

You can't insert data into a file. You can overwrite data at a specific location with RandomAccessFile. However an insert requires changing all of the data after it. For your case try something like this instead:

File file = new File("abhishek.txt");
Scanner scanner = new Scanner(file).useDelimiter("\n");
String line = scanner.next();
String newLine = line.substring(0, 5) + "Abhishek" + line.substring(5);
FileWriter writer = new FileWriter(file);
writer.write(newLine);
writer.close();
孤独陪着我 2024-11-10 05:29:07

一般来说,你需要读取旧文件,修改内存中的内容,然后再次写回。

这里有很多选项,例如是否一次读取全部文件,还是一次读取一小部分,是否替换现有文件等,但这通常是使用的模式。

Generally speaking you need to read the old file, modify the contents in memory and write it back out again.

There are many options here, as to whether you read the file all at once, or a small piece at a time, whether you replace the existing file, etc, but this is generally the pattern to use.

最美不过初阳 2024-11-10 05:29:07

我设法使用 RandomAccessFile 来做到这一点:

在此处输入图像描述

通过此操作,您将获得一个完全包含预期“我是 Abhishek”内容的文件。
即使您想将其保留在文件中的第一行之后的内容(这是我最初的问题:我有一个大文件,我必须在某个字符串之后插入一些内容)而不是在文件末尾,这更容易。

I managed to do it using RandomAccessFile:

enter image description here

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.

许一世地老天荒 2024-11-10 05:29:07

您有三种选择:

  1. 用足够的空间填充原始条目,以便可以使用 RandomAccessFile(并且您不关心一些额外的尾随空格。简单,但实用性有限。
  2. 如果您这样做,请使用模板引擎(StringTemplate、Velocity 等)如果您只执行一次,则可能需要做太多工作。
  3. 编写自己的简单模板引擎,将其读入内存并进行正则表达式替换,然后将其写回,但如果您的使用非常简单且有限,则可能会如此。最好的妥协。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文