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;



            }
          
           
           
        }
    }
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9240380/viewspace-719765/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9240380/viewspace-719765/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值