End of Stream encountered before parsing was completed.
Unhandled Exception: System.Runtime.Serialization.SerializationException: End ofStream encountered before parsing was completed.
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(He
aderHandler handler, __BinaryParser serParser, Boolean fCheck, IMethodCallMessag
e methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize
(Stream serializationStream, HeaderHandler handler, Boolean fCheck, IMethodCallM
essage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize
(Stream serializationStream)
at webcount.WebCount.ReadHashFromDisk()
at webcount.WebCount.Inital()
at webcount.WebCount.Main(String[] args)
程序启动时,读取上次已经写到硬盘上的序列化数据时出现上述问题,请高手指点!!!
类的定义:
[Serializable]
public class PortCount
{
public Hashtable htoIpTimeHash; // 存储IP和IP相应记录时间
public int nCount; // 存储总的访问次数
public PortCount()
{
htoIpTimeHash = new Hashtable();
nCount = 0;
}
}
下面函数用的pcoPortCountArray是全局变量,定义如下:
public static PortCount[] pcoPortCountArray = new PortCount[100];
向硬盘写数据的函数:
public static void WriteHashToDisk()
{
FileStream fsWrite = new FileStream(m_strBinaryFileName, FileMode.Create);
BinaryFormatter formatterIpCount = new BinaryFormatter();
formatterIpCount.Serialize(fsWrite, pcoPortCountArray);
fsWrite.Close();
}
从硬盘读取数据的函数:
public static void ReadHashFromDisk()
{
try
{
PortCount[] pcTmp;
FileStream fsRead = new FileStream(m_strBinaryFileName, FileMode.Open);
BinaryFormatter formatterIpCount = new BinaryFormatter();
pcTmp = (PortCount[])formatterIpCount.Deserialize(fsRead);
//fsRead.Close();
int nLen = pcTmp.Length;
for (int i=0; i<nLen; i++)
{
pcoPortCountArray[i].nCount = pcTmp[i].nCount;
pcoPortCountArray[i].htoIpTimeHash = (Hashtable)(pcTmp[i].htoIpTimeHash.Clone());
}
Console.WriteLine("clone ok");
fsRead.Close();
}
catch (System.IO.FileNotFoundException e)
{
Console.WriteLine("没有找到历史记录,开始创建新纪录");
}
catch (System.Exception e)
{
fsRead.Close();
Console.WriteLine("异常: {0}", e.Message);
}
} --------------------编程问答-------------------- 问题出现在这一行pcTmp = (PortCount[])formatterIpCount.Deserialize(fsRead);请高手指点,谢谢!! --------------------编程问答-------------------- 我也遇到同样的问题
--------------------编程问答-------------------- http://stackoverflow.com/questions/306596/end-of-stream-encountered-before-parsing-was-completed
PortCount[] pcTmp;
FileStream fsRead = new FileStream(m_strBinaryFileName, FileMode.Open);
fsRead.Position = 0;
BinaryFormatter formatterIpCount = new BinaryFormatter();
pcTmp = (PortCount[])formatterIpCount.Deserialize(fsRead);
补充:.NET技术 , C#