從二進位制檔案讀取和寫入



BinaryReaderBinaryWriter 類用於從二進位制檔案讀取和寫入二進位制檔案。

BinaryReader 類

BinaryReader 類用於從檔案讀取二進位制資料。透過將 FileStream 物件傳遞給其建構函式來建立 BinaryReader 物件。

下表描述了 BinaryReader 類的常用方法

序號 方法及描述
1

public override void Close()

它關閉 BinaryReader 物件和底層流。

2

public virtual int Read()

從底層流中讀取字元,並將流的當前位置向前移動。

3

public virtual bool ReadBoolean()

從當前流中讀取布林值,並將流的當前位置向前移動一個位元組。

4

public virtual byte ReadByte()

從當前流中讀取下一個位元組,並將流的當前位置向前移動一個位元組。

5

public virtual byte[] ReadBytes(int count)

從當前流中讀取指定數量的位元組到位元組陣列中,並將當前位置向前移動該位元組數。

6

public virtual char ReadChar()

從當前流中讀取下一個字元,並根據使用的編碼和從流中讀取的特定字元,將流的當前位置向前移動。

7

public virtual char[] ReadChars(int count)

從當前流中讀取指定數量的字元,將資料返回到字元陣列中,並根據使用的編碼和從流中讀取的特定字元,將當前位置向前移動。

8

public virtual double ReadDouble()

從當前流中讀取一個 8 位元組浮點數,並將流的當前位置向前移動八個位元組。

9

public virtual int ReadInt32()

從當前流中讀取一個 4 位元組有符號整數,並將流的當前位置向前移動四個位元組。

10

public virtual string ReadString()

從當前流中讀取字串。字串以長度為字首,以每次七位的方式編碼為整數。

BinaryWriter 類

BinaryWriter 類用於將二進位制資料寫入流。透過將 FileStream 物件傳遞給其建構函式來建立 BinaryWriter 物件。

下表描述了 BinaryWriter 類的常用方法。

序號 函式及描述
1

public override void Close()

它關閉 BinaryWriter 物件和底層流。

2

public virtual void Flush()

清除當前寫入器的所有緩衝區,並導致任何緩衝資料寫入底層裝置。

3

public virtual long Seek(int offset, SeekOrigin origin)

設定當前流中的位置。

4

public virtual void Write(bool value)

將一個位元組的布林值寫入當前流,其中 0 表示 false,1 表示 true。

5

public virtual void Write(byte value)

將一個無符號位元組寫入當前流,並將流的位置向前移動一個位元組。

6

public virtual void Write(byte[] buffer)

將位元組陣列寫入底層流。

7

public virtual void Write(char ch)

將一個 Unicode 字元寫入當前流,並根據使用的編碼和寫入流的特定字元,將流的當前位置向前移動。

8

public virtual void Write(char[] chars)

將字元陣列寫入當前流,並根據使用的編碼和寫入流的特定字元,將流的當前位置向前移動。

9

public virtual void Write(double value)

將一個八位元組浮點數寫入當前流,並將流的位置向前移動八個位元組。

10

public virtual void Write(int value)

將一個四位元組有符號整數寫入當前流,並將流的位置向前移動四個位元組。

11

public virtual void Write(string value)

以 BinaryWriter 的當前編碼將一個長度為字首的字串寫入此流,並根據使用的編碼和寫入流的特定字元,將流的當前位置向前移動。

有關方法的完整列表,請訪問 Microsoft C# 文件。

示例

以下示例演示了讀取和寫入二進位制資料 -

using System;
using System.IO;

namespace BinaryFileApplication {
   class Program {
      static void Main(string[] args) {
         BinaryWriter bw;
         BinaryReader br;
         
         int i = 25;
         double d = 3.14157;
         bool b = true;
         string s = "I am happy";
         
         //create the file
         try {
            bw = new BinaryWriter(new FileStream("mydata", FileMode.Create));
         } catch (IOException e) {
            Console.WriteLine(e.Message + "\n Cannot create file.");
            return;
         }
         
         //writing into the file
         try {
            bw.Write(i);
            bw.Write(d);
            bw.Write(b);
            bw.Write(s);
         } catch (IOException e) {
            Console.WriteLine(e.Message + "\n Cannot write to file.");
            return;
         }
         bw.Close();
         
         //reading from the file
         try {
            br = new BinaryReader(new FileStream("mydata", FileMode.Open));
         } catch (IOException e) {
            Console.WriteLine(e.Message + "\n Cannot open file.");
            return;
         }
         
         try {
            i = br.ReadInt32();
            Console.WriteLine("Integer data: {0}", i);
            d = br.ReadDouble();
            Console.WriteLine("Double data: {0}", d);
            b = br.ReadBoolean();
            Console.WriteLine("Boolean data: {0}", b);
            s = br.ReadString();
            Console.WriteLine("String data: {0}", s);
         } catch (IOException e) {
            Console.WriteLine(e.Message + "\n Cannot read from file.");
            return;
         }
         br.Close();
         Console.ReadKey();
      }
   }
}

編譯並執行上述程式碼後,將產生以下結果 -

Integer data: 25
Double data: 3.14157
Boolean data: True
String data: I am happy
csharp_file_io.htm
廣告