首頁 >後端開發 >C#.Net教程 >C# 二進位讀取器

C# 二進位讀取器

WBOY
WBOY原創
2024-09-03 15:22:40602瀏覽

在C#中,BinaryReader是一個用來處理二進位資料的類別。它位於 System.IO 命名空間下。 BinaryReader 用於將原始資料類型讀取為特定編碼流中的二進位值。 BinaryReader 使用 Stream 對象,也就是為了建立 BinaryReader 對象,我們需要在其建構子中傳遞 Stream 物件。 BinaryReader 類別有三個重載建構函式來處理二進位資料。預設情況下,BinaryReader 使用 UTF-8 編碼來讀取數據,直到我們在建立其物件時指定其他字元編碼。

文法及解釋

我們可以透過以下三種方式建立 BinaryReader 物件:

BinaryReader binary_reader = new BinaryReader(inputStream);

上面的語句根據指定的流(inputStream)使用UTF-8編碼初始化BinaryReader的新實例。

BinaryReader binary_reader = new BinaryReader(inputStream, encoding);

該語句根據指定的流(inputStream)和encoding指定的編碼來初始化BinaryReader的新實例。

BinaryReader binary_reader = new BinaryReader(inputStream, encoding, true);

該語句的工作方式與上面兩個語句相同,但有一個 Boolean 類型的額外參數,該參數用於指定使用者是否希望在 BinaryReader 物件被釋放後使流保持開啟狀態。此參數需要為“true”才能保持流打開,否則需要為“false”。

除了這三種方式之外,我們還可以使用以下語句建立BinaryReader:

using(BinaryReader binary_reader = new BinaryReader(File.Open(file_path, FileMode.Open)))
{
//user code
}

在上面的語句中,File.Open() 方法傳回 FileStream 的對象,因此它有助於建立 BinaryReader 的物件。

在「using」區塊內建立物件的好處是,當物件的工作完成且不再需要時,它會釋放該物件所持有的記憶體。

BinaryReader 在 C# 中如何運作?

BinaryReader 用於讀取二進位訊息,即用於讀取二進位檔案中儲存的資料。二進位檔案以機器可以輕鬆理解的方式儲存數據,但對於人類來說很難理解這些數據。為了幫助理解此類數據,使用了 BinaryReader。為了使用 BinaryReader,我們首先需要在程式碼中匯入 System.IO 命名空間。之後,我們需要藉助「new」運算子來建立 BinaryReader 的實例,並繞過 BinaryReader 建構子內的 Stream 物件。

在建立 BinaryReader 實例時,我們提供要讀取的流,如果不指定編碼,我們可以選擇指定要使用的字元編碼,預設使用 UTF-8 編碼。除此之外,我們還可以選擇指定是否希望在 BinaryReader 物件被釋放後開啟流,如下面的語句所示。

BinaryReader binary_reader = new BinaryReader(inputStream, encoding, true);

然後藉助BinaryReader針對不同資料型別提供的不同Read()方法,我們可以從檔案讀取資料。

BinaryReader 有許多支援不同資料類型的 Read() 方法,它們用於從流中讀取原始資料類型。例如 BinaryReader 的 ReadString() 方法用於讀取下一個位元組作為字串值,並將流中的目前位置前進一個位元組。

BinaryReader針對不同類型資料的Read()方法如下表:

Method Description
Read() It is used to read characters from an underlying stream and it also advances the current position of the stream according to the Encoding used and the specific character being read from the stream.
ReadBoolean() It is used to read the Boolean value from the stream and it also advances the current position of the stream by one byte.
ReadByte() It is used to read the next byte from the current stream and it also advances the current position of the stream by one byte.
ReadChar() It is used to read the next character from the current stream and it also advances the current position of the stream according to the Encoding used and the specific character being read from the stream.
ReadDecimal() It is used to read the decimal value from the current stream and it also advances the current position of the stream by sixteen bytes.
ReadDouble() It is used to read an 8-byte floating-point value from the current stream and advances the current position of the stream by eight bytes.
ReadInt32() It is used to read a 4-byte signed integer from the current stream and also it advances the current position of the stream by four bytes.
ReadString() It is used to read a string from the current stream.

Example of C# Binaryreader

Example of creating a file using BinaryWriter and reading it using BInaryReader.

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApp4
{
public class Program
{
string filePath = "E:\\Content\\binaryFile.dat";
public void WriteFile()
{
try
{
//checking if the file already exists
if (File.Exists(filePath))
{
File.Delete(filePath);
}
FileStream stream = new FileStream(filePath, FileMode.OpenOrCreate,
FileAccess.Write, FileShare.ReadWrite);
//creating binary file using BinaryWriter
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write("This is string");
writer.Write(100.53);
writer.Write(true);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public void ReadFile()
{
try
{
//creating an object of Stream
FileStream stream = new FileStream(filePath, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
//creating BinaryReader using Stream object
using (BinaryReader reader = new BinaryReader(stream))
{
//reading data using Read() methods of different data types
Console.WriteLine("String Value : " + reader.ReadString());
Console.WriteLine("Double Value : " + reader.ReadDouble());
Console.WriteLine("Boolean Value : " + reader.ReadBoolean());
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
public class BinaryReaderDemo
{
static void Main(string[] args)
{
Program obj = new Program();
obj.WriteFile();
obj.ReadFile();
Console.ReadKey();
}
}
}

Output:

C# 二進位讀取器

Conclusion

BinaryReader is used to read primitive data types as binary values in a specific encoding stream. If not defined explicitly, by default BinaryReader uses UTF-8 encoding to read data. Stream object needs to be passed inside the constructor of BinaryReader in order to create its instance.

以上是C# 二進位讀取器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn