从二进制文件拉动对象和投入列表< T> [英] Pulling objects from binary file and putting in List<T>

查看:106
本文介绍了从二进制文件拉动对象和投入列表< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样做过,但无法弄清楚如何我做到了。我有我想要存储的对象二进制文件。我用连载的对象类[Serializable接口] 并已成功地保存到文件中的各个对象吧。然而,问题开始显示出来试图读取对象并在目录及下放置时;> 作进一步处理。



于是试图只读取的对象出来的文件的时候,我的代码:

 列表<我的资料GT; aListOfStuff =新的List<我的资料GT;(); 
流FILESTREAM = File.OpenRead(MYFILE.DAT);
BinaryFormatter的解串器=新的BinaryFormatter();
aListOfStuff =(列表<我的资料GT;)deserializer.Deserialize(FILESTREAM);
fileStream.Close();

当我第一次开始测试,分别对上面的代码生成错误,但我会收到一个异常当试图写,因为这正在被另一个进程使用的文件不能被访问的文件。当试图通过注释掉代码的所有部分解决,将尝试检查文件已经存在的内容,只具有保存和放大器;刷新代码的主动,我会在代码aListofStuff行收到一个错误上面说无法投型的MyStuff对象键入'System.Collections.Generic.List'1。是的,1是错误,而不是一个错字。这告诉我,我得到了'文件仍然开放消息的原因是因为 .Close()没有被调用,但仍然无法帮助我找到问题的底部。



所以我假设我只是不知道/记得怎么拉的对象列表背出的文件。我搜索了如何做到这一点的网站并不能找到特别是关于对象的列表的信息。大量的工具和文件中的一个序列化对象,但不超过一个,我已经找到了序列化似乎并没有扩展这些条款。



下面是我使用一个对象添加到代码该文件:

 的MyStuff newThing =新的MyStuff(字符串1,字符串,STRING3); 
流FILESTREAM = File.Open (MYFILE.DAT,FileMode.Append);
BinaryFormatter的序列化=新的BinaryFormatter();
serializer.Serialize(FILESTREAM,newThing);
fileStream.Close();


解决方案

无法投类型的对象'的MyStuff来类型'System.Collections.Generic.List'1。



这意味着你序列化到文件中的一个的MyStuff对象,而不是对象列表。请确认。CNA还?提供的序列化代码



UPDATE2:



要序列化:

  VAR名单=新名单,LT;我的资料GT;(); 
变种objectOne =新的MyStuff(字符串1,字符串,STRING3);
//更多在这里对象
list.Add(objectOne);
// list.Add其它的目的使用

(流FILESTREAM = File.Open(MYFILE.DAT,FileMode.Create) )
{
BinaryFormatter的序列化=新的BinaryFormatter();
serializer.Serialize(FILESTREAM,清单);
}

要反序列化:

 列表<我的资料GT;清单; 
使用(流FILESTREAM = File.OpenRead(MYFILE.DAT))
{
BinaryFormatter的解串器=新的BinaryFormatter();
名单=(myStuffList<我的资料GT;)deserializer.Deserialize(FILESTREAM);
}


I've done this before, but can't figure out how I did it. I have a binary file that I want to store objects in. I've serialized the object class with [Serializable] and have successfully saved to the file individual objects to it. However, problems start showing up when trying to read the objects out and placing in a List<> for further handling.

So when trying to just read the objects out of the file, I have the code:

List<myStuff> aListOfStuff = new List<myStuff>();
Stream fileStream = File.OpenRead("MyFile.DAT");
BinaryFormatter deserializer = new BinaryFormatter();
aListOfStuff = (List<myStuff>)deserializer.Deserialize(fileStream);
fileStream.Close();

When I first started testing, no errors were generated on the above code, but I would receive an exception when trying to write to the file that the file couldn't be accessed because it was being used by another process. Upon trying to troubleshoot by commenting out all sections of code that would try to check the file for already-existing contents and only having the save & refresh code active, I would receive an error on the aListofStuff line of code above that stated "Unable to cast object of type 'myStuff' to type 'System.Collections.Generic.List'1. Yes, the 1 is in the error and not a typo. This tells me that the reason I was getting the 'file still open' message is because the .Close() wasn't being called but that still doesn't help me get to the bottom of the problem.

So I'm assuming I just don't know/remember how to pull a list of objects back out of a file. I've searched the web for how to do this and can't find information regarding specifically to lists of objects. Plenty of stuff for a single serialized object in a file, but not more than one and those articles that I've found for serializing don't seem to scale.

Here's the code I'm using to add an object to the file:

myStuff newThing = new myStuff(string1, string2, string3);
Stream fileStream = File.Open("MyFile.Dat", FileMode.Append);
BinaryFormatter serializer = new BinaryFormatter();
serializer.Serialize(fileStream, newThing);
fileStream.Close();

解决方案

"Unable to cast object of type 'myStuff' to type 'System.Collections.Generic.List'1.

This means that you serialized into File one myStuff object, not a list of objects. Check this. Cna you also provide serialization code?

UPDATE2:

To serialize:

var list = new List<myStuff>();
var objectOne = new myStuff(string1, string2, string3);
// more object here
list.Add(objectOne);
// list.Add other objects there

using(Stream fileStream = File.Open("MyFile.Dat", FileMode.Create))
{
  BinaryFormatter serializer = new BinaryFormatter();
  serializer.Serialize(fileStream, list);
}

To deserialize:

List<myStuff> list;
using(Stream fileStream = File.OpenRead("MyFile.DAT"))
{
  BinaryFormatter deserializer = new BinaryFormatter();
  list = (myStuffList<myStuff>)deserializer.Deserialize(fileStream);
}

这篇关于从二进制文件拉动对象和投入列表&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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