操作流對象

來源:互聯網
上載者:User
不管是MemoryStream還是FileStream,都是以位元組為讀寫資料的基本單位。而許多情況下應用程式都是以字元(佔兩個位元組)、數字(比如int,佔4個位元組)、String(若干字元的集合)來處理資料的。因此.NET提供了BinaryReader和BinaryWriter、StreamReader和StreamWriter來解決這個問題。

1. BinaryReader和BinaryWriter:
這兩個類主要用於向流中讀取和寫入各種基礎資料型別 (Elementary Data Type)的資料。
以下代碼建立檔案並寫入一些資料:private void BinaryWriteToFile()
        {
            //建立一個檔案流對象
            FileStream fs = new FileStream("test2.data",FileMode.Create);
            //將檔案流對象作為參數構造一個BinaryWriter對象
            BinaryWriter bw = new BinaryWriter(fs);
            //寫入int
            bw.Write(100);
            //寫入字元'A'
            bw.Write('A');
            //寫入double
            double d = 200.99;
            bw.Write(d);
            //寫入bool
            bw.Write(true);
            //關閉自身(隱含關閉它關聯的FileStream)
            bw.Close();
        }

BinaryWriter對象不能獨立存在,它必須依託FileStream或MemoryStream對象。
以下代碼按原先寫入的順序讀取資料:private void BinaryReadFormFile()
        {
            //建立一個檔案流對象
            FileStream fs = new FileStream("test2.data",FileMode.Open);    
            //將檔案流對象作為參數構造一個BinaryReader對象
            BinaryReader br = new BinaryReader(fs);
            //讀入int
            MessageBox.Show(br.ReadInt32().ToString());
            //讀入字元
            MessageBox.Show(br.ReadChar().ToString());
            //讀入double
            MessageBox.Show(br.ReadDouble().ToString());
            //讀入bool
            MessageBox.Show(br.ReadBoolean().ToString());
            //關閉自身(隱含關閉它關聯的FileStream)
            br.Close();
        }

2. StreamReader和StreamWriter:
BinaryWriter只能把字元和字元數組寫入流中,卻沒有提供寫入字串的方法,這個功能由StreamWriter類實現。
以下代碼建立一個StringData.txt,並向其中寫入兩個字串:private void WriteStringToFile()
        {
            FileStream fs = new FileStream("StringData.txt",FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine("合肥工業大學");
            sw.WriteLine("電子商務研究所");
            sw.Close();
        }

以下代碼是從檔案中分兩次讀取資料:private void ReadStringFromFile()
        {
            FileStream fs = new FileStream("StringData.txt",FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            MessageBox.Show(sr.ReadLine());
            MessageBox.Show(sr.ReadLine());
            sr.Close();
        }

還可以用ReadToEnd方法,可以一次性地從當前讀寫指標所指的位置開始讀到檔案結尾。private void ReadAllStringFromFile()
        {
            FileStream fs = new FileStream("StringData.txt",FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            MessageBox.Show(sr.ReadToEnd());
            sr.Close();
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.