码迷,mamicode.com
首页 > 其他好文 > 详细

序列化保存对象

时间:2016-01-14 00:48:27      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

 1 [Serializable]
 2     public class Student
 3     {
 4         public Student(int age)
 5         {
 6             this.age = age;
 7         }
 8 
 9         private int age;
10 
11         public string Name { get; set; }
12 
13     }
 1 static void Load()
 2         {
 3             using (FileStream fs = new FileStream("stu.bin", FileMode.Open))
 4             {
 5                 BinaryFormatter formatter = new BinaryFormatter();
 6                 Student stu = formatter.Deserialize(fs) as Student;
 7             }
 8 
 9             Console.WriteLine("读取成功");
10         }
11 
12         static void Save()
13         {
14             Console.WriteLine("请输入姓名!");
15             Student stu = new Student(10) { Name = Console.ReadLine() };
16             using (FileStream fs = new FileStream("stu.bin", FileMode.Create))
17             {
18                 BinaryFormatter formatter = new BinaryFormatter();
19                 formatter.Serialize(fs, stu);
20             }
21 
22             Console.WriteLine("保存成功");
23         }

 

序列化保存对象

标签:

原文地址:http://www.cnblogs.com/zhouzhuang/p/5128919.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!