1. 程式人生 > 其它 >C# 二進位制檔案的讀寫

C# 二進位制檔案的讀寫

BinaryReaderBinaryWriter類用於二進位制檔案的讀寫。

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# 文件。

例項

下面的例項演示了讀取和寫入二進位制資料:

例項

usingSystem;
usingSystem.IO;

namespaceBinaryFileApplication
{
classProgram
{
staticvoidMain(string[]args)
{
BinaryWriter bw;
BinaryReader br;
inti=25;
doubled=3.14157;
boolb=true;
strings="I am happy";
// 建立檔案
try
{
bw=newBinaryWriter(newFileStream("mydata",
FileMode.Create));
}
catch(IOException e)
{
Console.WriteLine(e.Message+"\nCannot create file.");
return;
}
// 寫入檔案
try
{
bw.Write(i);
bw.Write(d);
bw.Write(b);
bw.Write(s);
}
catch(IOException e)
{
Console.WriteLine(e.Message+"\nCannot write to file.");
return;
}

bw.Close();
// 讀取檔案
try
{
br=newBinaryReader(newFileStream("mydata",
FileMode.Open));
}
catch(IOException e)
{
Console.WriteLine(e.Message+"\nCannot 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+"\nCannot read from file.");
return;
}
br.Close();
Console.ReadKey();
}
}
}

當上面的程式碼被編譯和執行時,它會產生下列結果:

Integer data: 25
Double data: 3.14157
Boolean data: True
String data: I am happy

C# 檔案的輸入與輸出

1 篇筆記寫筆記

  1. 奇幻風之旅

    162***[email protected]

    7
    using System;
    using System.IO;
    
    namespace BinaryFileApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                BinaryWriter bw;
                BinaryReader br;
                int i = 25;
                double d = 3.14159;
                bool b = true;
                string s = "I am happy";
                #region
                //嘗試例項化,建立二進位制檔案
                try
                {
                    bw = new BinaryWriter(new FileStream("mydata", FileMode.Create));
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.Message + "\n Cannot create file.");
                    return;
                }
                #endregion
    
    
                try//嘗試寫入二進位制檔案
                {
                    bw.Write(i);
                    bw.Write(d);
                    bw.Write(b);
                    bw.Write(s);//寫入四行,每次寫入一行
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.Message + "\nCannot write to file.");
                    return;
                }
    
                bw.Close();//執行完寫入程式後關閉該二進位制檔案
    
                try//嘗試開啟該二進位制檔案
                {
                    br = new BinaryReader(new FileStream("mydata", FileMode.Open));
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.Message + "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 file.");
                    return;
                }
    
                br.Close();//讀取完後關閉該二進位制檔案
    
                Console.ReadKey();
            }
        }
    }
    奇幻風之旅

    奇幻風之旅

    162***[email protected]

    1年前 (2020-08-31)