c#二進位制讀取寫入流工具binaryreader_binarywriter電話薄示例

wisdomone1發表於2012-03-28
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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章