IO.StreamWriter随机停止写入 [英] IO.StreamWriter randomly stops writing

查看:55
本文介绍了IO.StreamWriter随机停止写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。



在一个连续将字符串记录到文本文件的应用程序中进行编码时,我注意到在某些时候,IO.StreamWriter类只会停止写作,即使它在一条线的中间。进一步研究,我发现在一次写一个长字符串时也会发生这种情况,例如这个代码的例子:

Hello there.

While coding in an application that continuosly logs strings to a text file, I noticed that at some point, the IO.StreamWriter class will simply stop writing, even if it''s in the middle of a line. Investigating further, I found out that this also happens when writing one long string at once, as this example code does:

Stream = New IO.FileStream(FilePath, _
                           IO.FileMode.Create, _
                           IO.FileAccess.Write)
Writer = New IO.StreamWriter(Stream)

Writer.Write(Space(30000))



如果我之后打开文本文件,而不是预期的30000个空格,我发现它只包含29696.有趣的是,无论我尝试编写多少个字符,它似乎总是多个实际上写的是1024.



有人知道问题是什么吗?


If I open the text file afterwards, instead of the expected 30000 spaces, I find that it contains only 29696. Interestingly, no matter how many characters I try to write, it always seems to be a multiple of 1024 that actually is written.

Does somebody have an idea what the issue could be?

推荐答案

问题解决了:)。写入字符串后不必关闭流,而是必须关闭编写器。否则,编写器只会写出其缓冲区的大小,显然是1024.



这样工作正常:

Problem solved :). Instead of closing the stream after the string is written, the writer has to be closed. Otherwise, the writer will only write the size of its buffer that apparently is 1024.

It works fine this way:
Stream = New IO.FileStream(FilePath, _
                           IO.FileMode.Create, _
                           IO.FileAccess.Write)
Writer = New IO.StreamWriter(Stream)
 
Writer.Write(Space(30000))
Writer.Close()


这篇关于IO.StreamWriter随机停止写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆