【发布时间】:2015-04-21 08:16:32
【问题描述】:
我正在使用以下代码:
var fileStream = new MemoryStream();
var binaryWriter = new BinaryWriter(fileStream);
var binaryReader = new BinaryReader(fileStream);
binaryWriter.Write("Hello");
var msg = binaryReader.ReadString();
但是我得到以下异常:
System.IO.EndOfStreamException: Unable to read beyond the end of the stream.
在读取之前,binaryReader.BaseStream.Length 大于 0,但 binaryReader.PeekChar() 返回 -1。
我做错了什么?
【问题讨论】:
-
我手头没有
C#编译器,但这可能是因为您没有将写入的数据Flushing 到底层设备。写信后尝试输入binaryWriter.Flush()。 -
Flush() 不起作用。问题确实出在职位上。
标签: c# serialization memorystream binaryreader binarywriter