C# 写入二进制文件

试验1

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);
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值