在 C# 中使用流写入器/读取器可以写入/读取的文本量是否有限制?
我正在尝试使用流写入器写入一些文本文件。 我试图写入的文本来自不同的文本文件。
我尝试:
string line = reader.ReadLine(); //reader is a streamReader I defined before
while (line != null)
{
sw.WriteLine(line); //sw is a streamWriter I defined before
line = reader.ReadLine();
}
我也尝试过:
while (!(reader.EndOfStream))
{
sw.WriteLine(reader.ReadLine()); //sw is a streamWriter I defined before
}
这两种方法成功地将文本从文件复制到另一个文件,但由于某种原因并未复制所有文本。 我尝试复制的文本文件非常大,大约有 96000 行,并且只复制了大约 95000 行。
因此,我问在 C# 中使用流写入器/读取器可以写入/读取的文本量是否有限制?
另外,我请求一些关于如何成功复制所有文本的建议。 (我读到有一个 Stream 类的方法副本,但那是针对 .NET4 的,所以它不会有帮助)。
编辑:我尝试将末尾未复制的文本替换为已复制开头的文本。我也遇到了同样的问题,所以不是角色的问题。
I am trying to write to some text file using a stream writer.
the text I am trying to write is from a different text file.
I try:
string line = reader.ReadLine(); //reader is a streamReader I defined before
while (line != null)
{
sw.WriteLine(line); //sw is a streamWriter I defined before
line = reader.ReadLine();
}
I also tried:
while (!(reader.EndOfStream))
{
sw.WriteLine(reader.ReadLine()); //sw is a streamWriter I defined before
}
this two methods succeeded to copy the text from the file to the other file, but from some reason not all of the text was copied.
The text file I am trying to copy from is very large, about 96000 lines, and only the ~95000 first lines are copied.
Therfore, I am asking if there is a restriction on the amount of text I can write / read with a stream writer / reader in C#?
Also, I asking for some suggestions for how to succeed copy all the text.
(I read that there is a method copy of the Stream class, but that is for .NET4, so it wont help).
EDIT: I tried to replace the text in the end that didn't copied by a text form the start that was copied. I got the same problem, so it isn't a problem with the characters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
唔。可能您没有刷新流。尝试执行 sw.Autoflush=true;或者,在关闭 sw 之前,调用 sw.Flush();
Hmm. Probably you are not flushing your stream. Try doing sw.Autoflush=true; Or, before you close sw, call sw.Flush();
我猜测您不会在输出流上调用刷新。这将导致最后几行(有时是很多行)不会写入输出文件。
I am going to guess that you are not calling flush on your output stream. This would cause the last few (sometimes a lot) of lines to not be written to the output file.