C# 我需要做 StreamWriter.flush() 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1059142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do I need to do StreamWriter.flush()?
提问by Nefzen
Suppose this C# code:
假设这个 C# 代码:
using (MemoryStream stream = new MemoryStream())
{
StreamWriter normalWriter = new StreamWriter(stream);
BinaryWriter binaryWriter = new BinaryWriter(stream);
foreach(...)
{
binaryWriter.Write(number);
normalWriter.WriteLine(name); //<~~ easier to reader afterward.
}
return MemoryStream.ToArray();
}
My questions are:
我的问题是:
- Do I need to use flush inside the loop to preserve order?
- Is returning
MemoryStream.ToArray()
legal? I using theusing
-block as a convention, I'm afraid it will mess things up.
- 我是否需要在循环内使用刷新来保持顺序?
- 回国
MemoryStream.ToArray()
合法吗?我使用using
-block 作为约定,我担心它会把事情搞砸。
采纳答案by Jon Skeet
Scratch the previous answer - I hadn't noticed that you were using two wrappers around the same stream. That feels somewhat risky to me.
从上一个答案开始——我没有注意到你在同一个流周围使用了两个包装器。这对我来说有点冒险。
Either way, I'd put the StreamWriter
and BinaryWriter
in their own using
blocks.
无论哪种方式,我都会将StreamWriter
和BinaryWriter
放在它们自己的using
块中。
Oh, and yes, it's legal to call ToArray()
on the MemoryStream
- the data is retained even after it's disposed.
哦,是的,调用是合法ToArray()
的MemoryStream
- 数据即使在处理后也会保留。
If you really want to use the two wrappers, I'd do it like this:
如果你真的想使用这两个包装器,我会这样做:
using (MemoryStream stream = new MemoryStream())
{
using (StreamWriter normalWriter = new StreamWriter(stream))
using (BinaryWriter binaryWriter = new BinaryWriter(stream))
{
foreach(...)
{
binaryWriter.Write(number);
binaryWriter.Flush();
normalWriter.WriteLine(name); //<~~ easier to read afterward.
normalWriter.Flush();
}
}
return MemoryStream.ToArray();
}
I have to say, I'm somewhat wary of using two wrappers around the same stream though. You'll have to keep flushing each of them after each operation to make sure you don't end up with odd data. You could set the StreamWriter
's AutoFlush
property to true to mitigate the situation, and I believethat BinaryWriter
currently doesn't actuallyrequire flushing (i.e. it doesn't buffer any data) but relying on that feels risky.
我不得不说,虽然我对在同一个流周围使用两个包装器有些警惕。您必须在每次操作后不断刷新它们中的每一个,以确保最终不会得到奇怪的数据。您可以在设定StreamWriter
的AutoFlush
属性为true来缓解的情况,我相信是BinaryWriter
目前并不实际需要潮红(即它不缓冲任何数据),但依托,感觉有风险的。
If you have to mix binary and text data, I'd use a BinaryWriter
and explicitly write the bytes for the string, fetching it with Encoding.GetBytes(string)
.
如果您必须混合二进制和文本数据,我会使用 aBinaryWriter
并显式写入字符串的字节,使用Encoding.GetBytes(string)
.
回答by mbillard
Update
更新
Nevermind this answer, I got confused with the writers...
没关系这个答案,我对作家感到困惑......
- No, the order will be preserved (update:maybe not). Flush is useful/needed in other situations, though I can't remember when.
- I think so, usingmakes sure everything cleans up nicely.
- 不,订单将被保留(更新:可能不会)。Flush 在其他情况下很有用/需要,但我不记得什么时候了。
- 我认为是这样,使用可以确保一切都很好地清理干净。