首頁  >  文章  >  後端開發  >  C# 中的二進位編寫器

C# 中的二進位編寫器

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

在 C# 中,BinaryWriter 是一個用於將原始型別寫入特定編碼流中的二進位資料的類別。它存在於 System.IO 命名空間下。

以下是有關 BinaryWriter 的一些要點:

  • BinaryWriter 用來建立二進位檔案。
  • BinaryWriter 可用於以特定編碼寫入字串。
  • 為了建立 BinaryWriter 的對象,我們需要將 Stream 的物件傳遞給 BinaryWriter 類別的建構子。
  • 建立 BinaryWriter 物件時,如果我們不指定任何編碼,則預設使用 UTF-8 編碼。

語法與解釋

建立 BinaryWriter 物件的建構子有四種重載形式。使用其所有重載建構函式建立 BinaryWriter 物件的語法如下:

文法 #1

protected BinaryWriter();

它用來初始化 BinaryWriter 類別的實例。

文法#2

BinaryWriter binaryWriter = new BinaryWriter(outputStream) ;

上面的語句根據指定的流(outputStream)並使用UTF-8字元編碼來初始化BinaryWriter類別的新實例。

文法#3

BinaryWriter binaryWriter = new BinaryWriter(outputStream, encoding);

上面的語句根據指定的流(outputStream)和字元編碼(encoding)初始化一個新的BinaryWriter實例。

文法#4

BinaryWriter binaryWriter = new BinaryWriter(outputStream, encoding, true);
  • 上面的語句與第二條和第三條語句類似,只是它有一個布林資料型別的額外參數,可以使用該參數來指示在 BinaryWriter 物件被釋放後是否要保持輸出流開啟。
  • 要使流保持開啟狀態,布林參數的值應設為“true”,否則應設為“false”。
  • 我們可以在using區塊中建立BinaryWriter類別的對象,這樣當該物件的工作完成並且不再需要時,該物件佔用的記憶體就會自動釋放。

代碼:

using (BinaryWriter binaryWriter = new BinaryWriter(File.Open(fileName, FileMode.Create )) )
{
//user code
}

這裡,File.Open() 方法傳回 FileStream 對象,有助於建立 BinaryWriter 的實例。

BinaryWriter 在 C# 中如何運作?

  • 在C#中,BinaryWriter用於將二進位資料寫入文件,或者我們可以說它用於創建二進位檔案。它幫助我們將二進位格式的原始資料類型寫入流。它還可以幫助我們以特定的字元編碼編寫字串。
  • 要使用BinaryWriter,需要在程式中匯入System.IO命名空間。然後,我們可以使用「new」運算子並繞過 Stream 物件到 BinaryWriter 的建構子來建立 BinaryWriter 類別的物件。
  • 為了建立 BinaryWriter 的實例,我們通常會向其建構函式提供 Stream 對象,同時我們可以提供一個可選參數來指定寫入檔案時要使用的編碼。如果使用者沒有提供任何字元編碼,則預設使用 UTF-8 編碼。
  • 還有另一個可選參數可以在建立 BinaryWriter 物件時傳遞給建構函式。此參數為布林類型,用於指定在 BinaryWriter 物件被釋放後使用者是否希望當前流保持開啟狀態。
  • BinaryWriter 類別為不同類型的資料提供了不同的 Write() 方法。這些方法用於將資料寫入二進位檔案。由於 Write(Int32) 方法用於將四位元組有符號整數寫入當前流,並且還將流位置前進四個位元組。

BinaryWriter 的方法

下表顯示了BinaryWriter針對不同資料類型的一些Write()方法的詳細資訊:

Method Description
Write(Boolean) This method is used to write the one-byte Boolean value to the present stream; 0 represents false while 1 represents true.
Write(Byte) This method is used to write an unsigned byte to the present stream and then it advances the position of the stream by one byte.
Write(Char) This method is used to write Unicode character to present stream and also it advances the present stream position according to the character encoding used and according to the characters being written to the present stream.
Write(Decimal) This method is used to write a decimal value to the present stream and also it advances the position of the current stream by sixteen bytes.
Write(Double) This method is used to write an eight-byte floating-point value to the present stream and then it also advances the position of the current stream by eight bytes.
Write(Int32) This method is used to write a four-byte signed integer to the present stream and then it advances the position of current stream by four bytes.
Write(String) This method is used to write length prefixed string to present stream in the present encoding of BinaryWriter and also it advances the current stream position according to the encoding used and according to the characters being written to the present stream.

Examples to Implement BinaryWriter in C#

Example showing the creation of file:

Code:

using System;
using System.IO;
namespace ConsoleApp4
{
public class Demo
{
string fileLocation = "E:\\Content\\newBinaryFile.dat";
public void WritingFile()
{
try
{
//checking if file exists
if (File.Exists(fileLocation))
{
File.Delete(fileLocation);
}
FileStream fileStream = new FileStream(fileLocation, FileMode.Create,
FileAccess.Write, FileShare.ReadWrite);
//creating binary file using BinaryWriter
using (BinaryWriter binaryWriter = new BinaryWriter(fileStream))
{
//writing data using different Write() methods
//of BinaryWriter
binaryWriter.Write(5253);
binaryWriter.Write("This is a string value.");
binaryWriter.Write('A');
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
public void ReadingFile()
{
try
{
FileStream fileStream = new FileStream(fileLocation, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
using (BinaryReader binaryReader = new BinaryReader(fileStream))
{
Console.WriteLine("IntegerValue = " + binaryReader.ReadInt32());
Console.WriteLine("StringValue = " + binaryReader.ReadString());
Console.WriteLine("CharValue = " + binaryReader.ReadChar());
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
}
public class BinaryWriterDemo
{
static void Main(string[] args)
{
Demo demoObj = new Demo();
demoObj.WritingFile();
demoObj.ReadingFile();
Console.ReadLine();
}
}
}

Output:

C# 中的二進位編寫器

Conclusion

In C#, the BinaryWriter class is used to write primitive types as binary information to the stream. If the encoding is not defined, then the BinaryWriter class uses the default UTF-8 character encoding to write data to a binary file. An object of BinaryWriter can be created using the Stream object.

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

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