17,741
社区成员
发帖
与我相关
我的任务
分享
class Student
{
private int id;
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public int Id
{
get { return id; }
set { id = value; }
}
}
class Program
{
static void Main(string[] args)
{
Student student1 = new Student();
Student student2 = new Student();
Student student3 = new Student();
student1.Id = 1;
student1.Name = "a";
student2.Id = 2;
student2.Name = "b";
student3.Id = 3;
student3.Name = "c";
ArrayList arraylist = new ArrayList();
arraylist.Add(student1);
arraylist.Add(student2);
arraylist.Add(student3);
//我怎么把arraylist中的对象放入txt中??
}
}
try
{
FileStream fs = new FileStream("feed.txt", FileMode.Create);
//2.构建二进制格式化对象
BinaryFormatter bf = new BinaryFormatter();
//3.调用序列化方法
bf.Serialize(fs, student1 );//把student1 对象保存到"feed.txt
fs.Close();
}
catch(Exception ex)
{
throw ex;
}
try
{
FileStream fs = new FileStream("feed.txt", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
feedFile = (FeedFile)bf.Deserialize(fs);
}
catch (Exception ex)
{}