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

    }

  • 相关阅读:
    jQuery入门教程
    vue-lazyload 图片不更新
    Eggjs 设置跨域请求
    Vue.js错误: Maximum call stack size exceeded
    ubuntu nginx ssl 证书配置
    ubuntu 安装nginx, 出现 Unable to locate package
    nginx 判断移动端或者PC端 进入不同域名
    node.js 生成二维码
    Linux 配置ssh 免密码登录
    nodejs 从部署到域名访问
  • 原文地址:https://www.cnblogs.com/suneryong/p/781818.html
Copyright © 2011-2022 走看看