将大字符串写入文件的最佳方法
在 C# 中,我正在读取中等大小的文件(100 KB ~ 1 MB),修改部分内容,最后写入不同的文件。所有内容均为文字。修改是作为字符串对象和字符串操作完成的。我当前的方法是:
- 使用 StreamReader 读取原始文件中的每一行。
- 打开一个
StringBuilder
来存放新文件的内容。 - 修改字符串对象并调用
StringBuilder
的AppendLine
(直到文件末尾) - 打开一个新的
StreamWriter
,并写入StringBuilder
到写入流。
但是,我发现 StremWriter.Write
截断了 32768 字节 (2^16),但 StringBuilder
的长度大于此值。我可以编写一个简单的循环来保证将整个字符串写入文件。但是,我想知道 C# 中完成此任务最有效的方法是什么?
总而言之,我只想修改文本文件的某些部分并写入不同的文件。但是,文本文件大小可能大于 32768 字节。
== 回答 == 很抱歉给您带来困扰!只是我没有调用flush
。 StremWriter.Write
没有短(例如,2^16)限制。
In C#, I'm reading a moderate size of file (100 KB ~ 1 MB), modifying some parts of the content, and finally writing to a different file. All contents are text. Modification is done as string objects and string operations. My current approach is:
- Read each line from the original file by using
StreamReader
. - Open a
StringBuilder
for the contents of the new file. - Modify the string object and call
AppendLine
of theStringBuilder
(until the end of the file) - Open a new
StreamWriter
, and write theStringBuilder
to the write stream.
However, I've found that StremWriter.Write
truncates 32768 bytes (2^16), but the length of StringBuilder
is greater than that. I could write a simple loop to guarantee entire string to a file. But, I'm wondering what would be the most efficient way in C# for doing this task?
To summarize, I'd like to modify only some parts of a text file and write to a different file. But, the text file size could be larger than 32768 bytes.
== Answer == I'm sorry to make confusin to you! It was just I didn't call flush
. StremWriter.Write
does not have a short (e.g., 2^16) limitation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该问题很可能与未关闭 writer 有关。请参阅 http://msdn.microsoft.com/en- us/library/system.io.streamwriter.flush.aspx。
但我建议如果可以避免的话,不要将整个文件加载到内存中。
The problem is most likely related to not closing the writer. See http://msdn.microsoft.com/en-us/library/system.io.streamwriter.flush.aspx.
But I would suggest not loading the whole file in memory if that can be avoided.
你能试试这个吗:
通过逐行处理并直接写入,可以避免将整个文件加载到内存中
can you try this :
It avoid to have to full file loaded in memory, by processing line by line and writing it directly
嗯,这完全取决于您要修改的内容。如果您对文本文件的一部分的修改依赖于文本文件的另一部分,那么显然您需要将这两个部分都存储在内存中。但是,如果您只需要逐行修改文本文件,则使用如下所示的内容:
Well, that entirely depends on what you want to modify. If your modifications of one part of the text file are dependent on another part of the text file, you obviously need to have both of those parts in memory. If however, you only need to modify the text file on a line-by-line basis then use something like this :
我不会使用洞文档来查找您正在寻找的内容,而是使用正则表达式来查找示例:
Instead of of running though the hole dokument i would use a regex to find what you are looking for Sample:
StreamWriter.Write
不会
截断字符串,并且没有限制。
它在内部使用
String.CopyTo
,另一方面使用不安全代码(使用fixed
)来复制字符,因此它是最高效。StreamWriter.Write
does not
truncate the string and has no limitation.
Internally it uses
String.CopyTo
which on the other hand uses unsafe code (usingfixed
) to copy chars so it is the most efficient.