c#二进制读取写入流工具binaryreader_binarywriter电话薄示例

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    //声明结构体在phonebook类中调用
    struct Record
    {
        public string name;
        public int age;//uint16是结构体类型哟
        public string phone;
        public string address;
    }
    class Phonebook
    {
        FileStream f_stream;//文件流变量
        string s_filename;//文件变量,用于构造文件流
        Record m_record;//在类phonebook中使用结构体record,结构体就是一种类型
        public Phonebook(string filename)
        {
            s_filename = filename;
        }
        //电话薄的显示,添加,删除(显示包含各个操作子项的展示)

        //初始界面显示
        public char open()
        {
            //初始化显示电话薄各个操作功能界面
            Console.WriteLine("1:显示记录"); ;
            Console.WriteLine("2:添加新的记录"); ;
            Console.WriteLine("3:删除记录"); ;
            Console.WriteLine("4:退出");
            //int i = Console.Read();//上述不同操作子项数字的值源自终端键盘输入的接收
             上述代码不能捕获到1,2,3,4,可以用如下方法替代
            char i = (char)Console.Read();
            return i;
        }
        //添加新的记录
        public void Addnew()
        {
            //初始化文件流,用于添加新记录
            try
            {
                f_stream = new FileStream(s_filename,FileMode.OpenOrCreate);
            }
            catch //不带括号()可以抓取所有的异常
            {
                Console.WriteLine("电话薄出错");
                return;
            }

            //通过二进制binarywriter写入流进行添加新记录
            //BinaryWriter是对filestream的封装
            BinaryWriter bw = new BinaryWriter(f_stream);
            //binarywriter.seek定位当前流的位置,参数分别为偏移量offset,它是相对于第二个参数seekorigin,seekorigin它是偏移量的参考
            //即以第二个参数为基准,流的位置移动到哪里,这个哪里由offset控制
            bw.Seek(0,SeekOrigin.End);//此代码表明移动到当前流的末尾
            //下面开始通过binarywriter二进制流写入工具把信息写入到电话薄中
            string s_temp;
            Console.Write("name:");
            Console.ReadLine();
            m_record.name = Console.ReadLine();
            bw.Write(m_record.name);

            reenter:
            Console.Write("age:");

            //s_temp定义为string是为了最终插入uint16类型的age结构体成员
            s_temp = Console.ReadLine();
            try
            {
                m_record.age = Convert.ToInt16(s_temp);
            }
            catch
            {
                Console.WriteLine("age格式错误,请重新输入");
                goto reenter;
            }

            bw.Write(m_record.age);

            Console.Write("phone number:");
            //先用标准输入把电话信息存储结构体中
            m_record.phone = Console.ReadLine();
            //然后通过二进制流写入工具把结构体信息写入到流中
            bw.Write(m_record.phone);

            Console.Write("address:");
            //先用标准输入把电话信息存储结构体中
            m_record.address = Console.ReadLine();
            //然后通过二进制流写入工具
            bw.Write(m_record.address);
            f_stream.Close();//为何只关闭文件流filestream,binarywriter为何不关闭
            Console.WriteLine("电话薄记录添加完毕");

        }


        //显示所有的记录
        public void List()
        {
            try
            {
                f_stream = new FileStream(s_filename, FileMode.Open);
            }
            catch
            {
                Console.WriteLine("电话薄出错");
                return;
            }
            BinaryReader br = new BinaryReader(f_stream);
            Console.WriteLine("name   age   phone number     address");
            Console.WriteLine("*************************************");
            while (true) //while条件,条件在括号()中
            {
                try
                {
                    //binaryreader.readstring是何义
                    m_record.name = br.ReadString();
                    //必须用readint32与age的int类型匹配,不然取到值为空
                    m_record.age = br.ReadInt32();
                    m_record.phone = br.ReadString();
                    m_record.address = br.ReadString();

                }
                catch
                {
                    return;
                }
                Console.WriteLine("{0,-9},{1,-6},{2,-18},{3}",m_record.name,m_record.age,m_record.phone,m_record.address);
            }
            //f_stream.Close();
            Console.WriteLine("显示电话薄记录完毕");
            Console.ReadKey();
        }


        public void Erase()
        {
            try
            {
                f_stream = new FileStream(s_filename, FileMode.Create);

            }
            catch
            {
                Console.WriteLine("电话薄出错");
                return;
            }
            f_stream.Close();
            Console.WriteLine("现在电话薄是空的了");
        }
    }
}



using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Phonebook pb = new Phonebook(@"c:\testphone.txt");
            char i=pb.open();
            switch(i)
            {
                case '1':
                    pb.List();
                    break;
                case '2':
                    pb.Addnew();
                    break;
                case '3':
                    pb.Erase();
                    break;



            }
          
           
           
        }
    }
}

请使用浏览器的分享功能分享到微信等