C# - 随机写入文件 - 在第一行之前写入第二行

发布于 2024-11-02 11:03:14 字数 701 浏览 6 评论 0原文

我正在尝试使用 FileStream 写入文件,并且想要写入第二行,然后写入第一行。我在编写第二行后使用 Seek() 返回开头,然后编写第一行。它替换第二行(或其中一部分,具体取决于第一行的长度。)我如何不让它替换第二行?

        var fs = new FileStream("my.txt", FileMode.Create);
        byte[] stringToWrite = Encoding.UTF8.GetBytes("string that should be in the end");
        byte[] stringToWrite2 = Encoding.UTF8.GetBytes("first string\n");
        fs.Write(stringToWrite, 0, stringToWrite.Length);
        fs.Seek(0, SeekOrigin.Begin);
        fs.Write(stringToWrite2, 0, stringToWrite2.Length);

文件中写入以下内容:

first string
hould be in the end

我希望它是

first string
string that should be in the end

谢谢

I am trying to write to a file using a FileStream and want to write the second line and then write the first line. I use Seek() to go back to the beginning after writing the second line and then write the first line. It replaces the second line ( or part of it depending on the length of the first line.) How do I not make it replace the second line?

        var fs = new FileStream("my.txt", FileMode.Create);
        byte[] stringToWrite = Encoding.UTF8.GetBytes("string that should be in the end");
        byte[] stringToWrite2 = Encoding.UTF8.GetBytes("first string\n");
        fs.Write(stringToWrite, 0, stringToWrite.Length);
        fs.Seek(0, SeekOrigin.Begin);
        fs.Write(stringToWrite2, 0, stringToWrite2.Length);

Following is written to the file:

first string
hould be in the end

I want it to be

first string
string that should be in the end

Thanks

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

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

发布评论

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

评论(3

倾`听者〃 2024-11-09 11:03:14

您首先需要在文件中查找等于第一个字符串的长度。

fs.Seek(stringToWrite2.Length, SeekOrigin.Begin);
fs.Write(stringToWrite, 0, stringToWrite.Length);
fs.Seek(0, SeekOrigin.Begin);

另外,不要忘记处置您的流(using)。

You first need to seek into the file equal to the length of the first string.

fs.Seek(stringToWrite2.Length, SeekOrigin.Begin);
fs.Write(stringToWrite, 0, stringToWrite.Length);
fs.Seek(0, SeekOrigin.Begin);

Also, don't forget to dispose of your stream (using).

山有枢 2024-11-09 11:03:14

您无法插入文件并将现有内容推回。您只能覆盖或扩展。

因此,在您知道文件前面所有内容的内容之前,您无法写入该文件的一部分。

You can't insert into a file and push the existing contents back. You can only overwrite or extend.

You therefore can't write a piece of the file until you know the contents of all that precedes it.

注定孤独终老 2024-11-09 11:03:14

根据您想要实现的目标,您可能需要写入两个不同的文件,其中一个是临时文件。

  1. 创建一个包含“第二”内容的临时文件
  2. 创建一个包含“第一”内容的新文件
  3. 打开第一个文件,读取其内容并附加到第二个文件

如果这是更大的解决方案中反复出现的要求,也许您想要的是某种数据库的?也许是基于文件的数据库,例如 SqlLiteBerkeleyDb

此处讨论了类似的问题。

Depending on what you are trying to achieve, you may need to write to two different files, one being a temporary file.

  1. Create a temporary file with "second" content
  2. Create a new file with the "first" content
  3. Open the first file, read its content and append to the second file

If this a recurring requirement in a bigger solution, maybe what you want is some kind of database? Maybe a file-based database like SqlLite or BerkeleyDb.

A similar problem is discussed here.

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