zoukankan      html  css  js  c++  java
  • 二进制序列化和反序列化类

    using System;
    using System.IO;
    using System.Runtime.Serialization; //二进制序列化类的命名空间
    using System.Runtime.Serialization.Formatters.Binary;//提供的是二进制的命名空间
    namespace Console88
    {
     /// <summary>
     /// Class1 的摘要说明。
     /// </summary>
     public class Class1
     {
      /// <summary>
      /// 应用程序的主入口点。
      /// </summary>
      [STAThread] //表示的是单进程的
      static void Main(string[] args)
      {
       //
       // TODO: 在此处添加代码以启动应用程序
       //
       Student s=new Student();

       s.i=100;

       s.j=200;

       //IFormatter 提供将序列化对象格式化的功能

       IFormatter  ff=new BinaryFormatter();//以二进制序列化类

    //   Stream stream=new FileStream(@"MyFile.txt",FileMode.Create,FileAccess.Write,FileShare.None);//将文件写入流
      
    //   ff.Serialize(stream,s);//将对象提供给所序列化的流


       Stream stream=new FileStream(@"MyFile.txt",FileMode.Open,FileAccess.Read,FileShare.Read);//反序列化的文件必须存在

       stream.Seek(0,SeekOrigin.Begin); //解决的是反序列化的时候解决流的结尾

       ff.Deserialize(stream); //反序列化该类得流
       
       stream.Close();//关闭并释放所有的资源

       Console.WriteLine("i:{0}",s.i);

       Console.Write("j:{0}",s.j);

       }
     }

     [Serializable] //序列化该类
     public class Student
     {
      public int i=0;
      public int j=0;
     }

    }

  • 相关阅读:
    作业:ATM
    软件开发目录规范
    re模块
    logging模块
    ConfigParser模块&hashlib模块&subprocess模块
    json模块&pickle模块&shelve模块&xml模块
    时间模块time&datetime
    vue里面render详细写法
    node.js创建服务
    vue退出功能的实现
  • 原文地址:https://www.cnblogs.com/suneryong/p/781818.html
Copyright © 2011-2022 走看看