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

}

相关文章:

  • 2022-12-23
  • 2021-12-19
  • 2021-08-22
  • 2022-12-23
  • 2020-06-16
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-09
  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
  • 2021-07-01
  • 2022-12-23
相关资源
相似解决方案