C# 二進位檔的讀寫
BinaryReader 和 BinaryWriter 類用於二進位檔的讀寫。
BinaryReader 類
BinaryReader 類用於從檔讀取二進位數據。一個 BinaryReader 對象通過向它的構造函數傳遞 FileStream 對象而被創建。
下表列出了 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()
從當前流中讀取一個字串。字串以長度作為首碼,同時編碼為一個七位的整數。 |
如需查看完整的方法列表,請訪問微軟的 C# 文檔。
BinaryWriter 類
BinaryWriter 類用於向檔寫入二進位數據。一個 BinaryWriter 對象通過向它的構造函數傳遞 FileStream 對象而被創建。
下表列出了 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
)
把一個 8 位元組浮點值寫入到當前流中,並把流位置往前移八個位元組。 |
| 10 | public virtual void Write(
int value
)
把一個 4 位元組有符號整數寫入到當前流中,並把流位置往前移四個位元組。 |
| 11 | public virtual void Write(
string value
)
把一個以長度為首碼的字串寫入到 BinaryWriter 的當前編碼的流中,並把流的當前位置按照所使用的編碼和要寫入到流中的指定的字元往前移。 |
如需查看完整的方法列表,請訪問微軟的 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";
// 創建檔
try
{
bw = new BinaryWriter(new FileStream("mydata",
FileMode.Create));
}
catch (IOException e)
{
Console.WriteLine(e.Message + "\n Cannot create file.");
return;
}
// 寫入檔
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();
// 讀取檔
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

C# 檔的輸入與輸出