C#之Stream和IO

C#之Stream和IO

public class test
    {
        public int aa = 100;
        public string testStr = "test";
        public void Save(BinaryWriter bw)
        {
            bw.Write(aa);
            bw.Write(testStr);
        }
        public void Load(BinaryReader br)
        {
            aa = br.ReadInt32();
            testStr = br.ReadString();
        }
    }

public class Program
    {
        public static void Main()
        {
            var a = new test() { aa = 1000, testStr = "yjw" };
            using(var fs=File.OpenWrite("johnyang.dat"))
            using(var bw=new BinaryWriter(fs))
            {
                a.Save(bw);
            }
            var b = new test();
            using(var fs=File.OpenRead("johnyang.dat"))
            using(var br=new BinaryReader(fs))
            {
                b.Load(br);
            }
            Console.WriteLine(b.testStr);
            Console.WriteLine(b.aa);
        }
}

output:

yjw
1000

未完待续...

posted @ 2022-01-19 09:13  JohnYang819  阅读(44)  评论(0编辑  收藏  举报