zoukankan      html  css  js  c++  java
  • BinaryReader 和BinaryWriter 读写类对象

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    
    namespace ConsoleApplication1
    {
         
            public class INFO
            {
                public Int32 a { get; set; }
                public string b { get; set; }
                public string c { get; set; }
    
                public INFO()
                {
                    a = 0;
                    b = "";
                    c = "";
                }
    
               
    
                public void ReadFromStream(Stream ms)
                {
                  
                    BinaryReader binReader = new BinaryReader(ms);
                    this.a = binReader.ReadInt32();
                    this.b = binReader.ReadString();
                    this.c = binReader.ReadString();
                }
    
                public void WriteToStream(Stream ms)
                {
                    BinaryWriter binWriter = new BinaryWriter(ms);
                    binWriter.Write(this.a);
                    binWriter.Write(this.b);
                    binWriter.Write(this.c);
    
    
                    Console.WriteLine(this.a.ToString());
                    Console.WriteLine(this.b);
                    Console.WriteLine(this.c);
                    
                    
                }
    
                static void Main(string[] args)
                {
    
    
                    const string FilePath = @"D:a.txt";
                    FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    INFO g = new INFO();
                    g.a = 12;
                    g.b = "abcdefa";
                    g.c = "小圆子";
    
                    g.WriteToStream(fs);
                 
                    fs.Close();
    
    
    
                    FileStream fsr = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    g.ReadFromStream(fsr);
                    fsr.Close();
                    Console.ReadLine();
                }
            }
    
    
        }
  • 相关阅读:
    包含min函数的栈
    树的子结构
    合并两个排序的链表
    反转链表
    字符移位
    有趣的数字
    顺时针打印矩阵
    有道云笔记自动签到
    Shell重定向
    Ubuntu 18.04安装Docker
  • 原文地址:https://www.cnblogs.com/zzlp/p/3247542.html
Copyright © 2011-2022 走看看