zoukankan      html  css  js  c++  java
  • 序列化

        class Program
        {
            static void Main(string[] args)
            {
                //需要将对象的状态保存起来  持久化
                //序列化:  将对象的状态持久化到某1中设备上(磁盘.)
                //要将类标记为Serializable 这个类的对象才可以被序列化
                //以二进制的方式序列化  而不是文本文档.
                //反序列化.  将之前序列化的文件还原为对象 
    
                //Person p = new Person() { Age = 12, Name = "rose" };
                //System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                //using (FileStream fs = new FileStream("se.bin", FileMode.Create))
                //{
                //    bf.Serialize(fs, p);
                //}
                //极品飞车 
    
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                using (FileStream fs = new FileStream("se.bin", FileMode.Open))
                {
                    object obj = bf.Deserialize(fs);
                    Person p = obj as Person;
                    Console.WriteLine(p.Name + ":" + p.Age);
                } 
    
                //Person p = new Person();
                //if (File.Exists("1.txt"))
                //{
                //    string[] lines = File.ReadAllLines("1.txt", Encoding.Default);
                //    p.Age = int.Parse(lines[0]);
                //    p.Name = lines[1];
                //}
                //else
                //{ 
                //    p.Age = 17;
                //    p.Name = "jack"; 
                //    File.WriteAllLines("1.txt", new string[] { p.Age.ToString(), p.Name });
                //} 
                //Console.WriteLine(p.Age+":"+p.Name); 
    
                Console.ReadKey();
            }
        }
    

      

  • 相关阅读:
    linux防火墙关闭与中文显示乱码排错
    linux基础命令
    盒子模块
    表的数据类型
    pymysql模块
    sql综合练习题
    pymysql内置功能
    数据操作
    vue 左侧菜单展示,以及对应的路由配置
    vue 左侧菜单路由实现
  • 原文地址:https://www.cnblogs.com/sumg/p/3754077.html
Copyright © 2011-2022 走看看