c# BinaryWriter 和 BinaryReader

 

 

string path = @"C:\Users\Administrator\Desktop\1.txt";
                using (FileStream ws = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    using (BinaryWriter bw = new BinaryWriter(ws))
                    {
                        bw.Write(123);
                        bw.Write(true);
                        bw.Write("hello world");
                    }
                }

                using (FileStream rs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    using (BinaryReader br = new BinaryReader(rs))
                    {
                        //读取的顺序必须和写入的顺序一致
                        int i = br.ReadInt32();
                        bool b = br.ReadBoolean();
                        string s = br.ReadString();
                        Console.WriteLine(i);
                        Console.WriteLine(b);
                        Console.WriteLine(s);
                    }
                }

 

posted @ 2018-03-04 23:57  热敷哥  阅读(450)  评论(0编辑  收藏  举报