C# ArrayList用BinaryFormatter序列化和反序列化进行文件读写的一个简单例子
来源:互联网 发布:国美淘宝 编辑:程序博客网 时间:2024/05/29 06:47
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO; //文件读写的IO空间
using System.Runtime.Serialization.Formatters.Binary; //BinaryFormatter序列化需要的空间
//Person类
namespace Serializable
{
[Serializable] //可序列化的声明,一定要有,不然无法序列化
class Person
{
private String name;
private String sex;
private int age;
public Person(String n, String s, int a)
{
name = n;
sex = s;
age = a;
}
public String Name
{
get
{
return name;
}
set
{
name = value;
}
}
public String Sex
{
get
{
return sex;
}
set
{
sex = value;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
}
}
//主运行程序
namespace Serializable
{
class Program
{
static void Main(string[] args)
{
Person p1 = new Person("张三", "男", 20);
Person p2 = new Person("John", "男", 20);
ArrayList al = new ArrayList();
al.Add(p1);
al.Add(p2);
//序列化
try
{
FileStream fs = new FileStream("serialiable.bin", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, al);
fs.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("序列化成功!");
Console.Read();
//反序列化
try
{
FileStream fs = new FileStream("serialiable.bin", FileMode.Open, FileAccess.Read);
BinaryFormatter bf = new BinaryFormatter();
ArrayList all = (ArrayList)bf.Deserialize(fs);
foreach (Person p in all)
{
Console.WriteLine(p.Name + p.Sex + p.Age);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.Read();
Console.Read();
}
}
}
运行结果:
- C# ArrayList用BinaryFormatter序列化和反序列化进行文件读写的一个简单例子
- C# 类用XmlSerializer进行序列化和反序列化进行文件读写的一个简单例子
- C# 二进制BinaryFormatter进行序列化与反序列化
- C# 二进制BinaryFormatter进行序列化与反序列化
- MSDN|BinaryFormatter|序列化和反序列化|c#
- BinaryFormatter 序列化反序列化
- BinaryFormatter 序列化及反序列化
- 二进制BinaryFormatter 泛型 序列化与反序列化 (保存文件到本地和读取)
- BinaryFormatter.Deserialize 不能反序列化其它Application或者assembly产生的序列化文件
- c#简单的序列化和反序列化
- 序列化和反序列化的一个简单练习
- C# 二进制读写与序列化和反序列化
- 使用C#语言进行序列化和反序列化
- BinaryFormatter序列化与反序列化(C#)
- google protocl buffer 序列化和反序列化的一个例子
- google protocl buffer 序列化和反序列化的一个例子
- 序列化和反序列化的例子 程序中新建文件的操作
- .NET进行Json的序列化和反序列化
- Linux中的远程连接 SSH and VNC图形模式
- 目标函数极值求解的几种方法
- 帖一篇文章,免得大家失望
- 怎样用matlab插值得到函数表达式
- ORACLE创建用户、角色及权限管理
- C# ArrayList用BinaryFormatter序列化和反序列化进行文件读写的一个简单例子
- web的分页
- 使用ACT(Microsoft Application Center Test) 进行压力测试
- iphone程序的生命周期分析
- 在eclipse中调试ant中的junit测试代码
- 我的C++编程命名规范
- MySql 安装PhpMyAdmin 备份数据库
- PHP使用PEAR类发送邮件程序代码
- Wininet CInternetSession类编程的一些心得 包括如何清空cookie