什么是用C#/。NET替换文件文本的最佳方式?文本、方式、文件、NET

2023-09-04 00:26:07 作者:赏烟花じ飞满天

我有正被写入作为一个非常大的数据提取的一部分的文本文件。文本文件的第一行是提取的账户的数量。

I have a text file that is being written to as part of a very large data extract. The first line of the text file is the number of "accounts" extracted.

,该号码是未知的,直到该过程的最后,但文件可以是大(几百兆)。

Because of the nature of this extract, that number is not known until the very end of the process, but the file can be large (a few hundred megs).

什么是C#/。NET的最佳方式打开一个文件(在这种情况下,一个简单的文本文件),并替换为文本的第一个行中的数据?

What is the BEST way in C# / .NET to open a file (in this case a simple text file), and replace the data that is in the first "line" of text?

重要提示: - 我不需要更换字节固定金额 - 这很容易。这里的问题是,需要在该文件的顶部插入的数据是可变的。

IMPORTANT NOTE: - I do not need to replace a "fixed amount of bytes" - that would be easy. The problem here is that the data that needs to be inserted at the top of the file is variable.

重要注意事项2 : - 有几个人询问/提及只须保持在内存中的数据,然后取代它......不过这是完全不可能的。为什么这个过程正在更新的原因是因为在加载一些演出到内存中时,有时会崩溃的事实。

IMPORTANT NOTE 2: - A few people have asked about / mentioned simply keeping the data in memory and then replacing it... however that's completely out of the question. The reason why this process is being updated is because of the fact that sometimes it crashes when loading a few gigs into memory.

推荐答案

如果你能,你应该插入您覆盖在年底的实际数量和空间占位符。

If you can you should insert a placeholder which you overwrite at the end with the actual number and spaces.

如果这不是一个选项,先写你的数据缓存文件。当你知道的实际数量创建输出文件,并从缓存中追加数据。

If that is not an option write your data to a cache file first. When you know the actual number create the output file and append the data from the cache.