using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace 创建二进制文件
{
[Serializable]
class Struct
{
public string a { get; set; }
public string b { get; set; }
public int s { get; set; }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("创建二进制文件");
Save();
Read();
Console.ReadKey();
}
static void Save()
{
Struct mystruct = new Struct();
mystruct.a = "123";
mystruct.b = "ABCD";
mystruct.s = 27;
using (FileStream fileStream = new FileStream(@"abc.txt", FileMode.OpenOrCreate))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream, mystruct);
}
}
static void Read()
{
using (FileStream fileStream = new FileStream(@"abc.txt", FileMode.OpenOrCreate))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
Struct mystruct = binaryFormatter.Deserialize(fileStream) as Struct;
Console.WriteLine(mystruct.a);
Console.WriteLine(mystruct.b);
Console.WriteLine(mystruct.s);
}
}
}
}
试验2-序列化列表
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
namespace 创建二进制文件
{
[Serializable]
public class A
{
public A(int a)
{
this.a = a;
}
public int a;
}
class 序列化列表
{
static void Main(string[] args)
{
Console.WriteLine("创建二进制文件-list");
序列化列表 p = new 序列化列表();
p.mian();
Console.ReadKey();
}
public void mian() {
Save();
Read();
}
public void Save()
{
List<A> list = new List<A>();
for(int i = 0; i < 5; i++)
{
A a = new A(i);
list.Add(a);
}
using (FileStream fileStream = new FileStream(@"a.txt", FileMode.OpenOrCreate))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream, list);
}
}
static void Read()
{
using (FileStream fileStream = new FileStream(@"a.txt", FileMode.OpenOrCreate))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
List<A> list = binaryFormatter.Deserialize(fileStream) as List<A>;
for (int i = 0; i < 5; i++)
{
Console.WriteLine(list[i].a);
}
}
}
}
}
试验3-复杂对象
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace 创建二进制文件
{
[Serializable]
public class B
{
public B(int b)
{
a = b;
}
public int a;
}
[Serializable]
public class C
{
public List<B> bs = new List<B>();
public int b;
}
class 序列化综合对象
{
static void Main(string[] args)
{
Console.WriteLine("创建二进制文件-综合对象");
序列化综合对象 p = new 序列化综合对象();
p.mian();
Console.ReadKey();
}
public void mian()
{
Save();
Read();
}
public void Save()
{
C c = new C();
c.b = 5;
for (int i = 0; i < 5; i++)
{
B b = new B(i);
c.bs.Add(b);
}
using (FileStream fileStream = new FileStream(@"b.txt", FileMode.OpenOrCreate))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream, c);
}
}
static void Read()
{
using (FileStream fileStream = new FileStream(@"b.txt", FileMode.OpenOrCreate))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
C c = binaryFormatter.Deserialize(fileStream) as C;
for (int i = 0; i < 5; i++)
{
Console.WriteLine(c.bs[i].a);
}
}
}
}
}