C# ArrayList用BinaryFormatter序列化和反序列化进行文件读写的一个简单例子
2011-04-22 11:04
826 查看
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();
}
}
}
运行结果:
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# 类用XmlSerializer进行序列化和反序列化进行文件读写的一个简单例子
- Android读写文件的一个简单入门的例子
- 用c#进行directX开发的一个简单例子
- 一个经过改良的XMLHelper(包含了序列化,反序列化,创建xml文件,读取节点,C#对象与xml文件的相互转换等等。)
- 谁能在同一文件序列化多个对象并随机读写(反序列化)?BinaryFormatter、SoapFormatter、XmlSerializer还是BinaryReader
- 【转载】关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 创建文件
- 【转载】关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 读取文件
- 【转载】关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 高级操作
- 关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用)
- 用c#进行directX开发的一个简单例子
- 一个简单的文件读写、复制的例子
- 关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 创建文件
- 关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 读取文件
- 关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 高级操作
- 格式转换------用C#代码生成一个简单的PDF文件
- python编程练习----对文件进行简单的操作(读写)
- c#用一个线程同步的简单例子
- c#中进行读写配置文件的方法
- 读出一个文本文件,并在前面加上行号后,写入到另一个文件的简单例子
- 一个简单的C#多线程间同步的例子