ホームページ  >  記事  >  バックエンド開発  >  C# の BinaryWriter

C# の BinaryWriter

WBOY
WBOYオリジナル
2024-09-03 15:22:35239ブラウズ

C# では、BinaryWriter は、特定のエンコード ストリームでプリミティブ型をバイナリ データとして書き込むために使用されるクラスです。これは System.IO 名前空間の下に存在します。

次に、BinaryWriter に関する重要な点をいくつか示します:

  • BinaryWriter はバイナリ ファイルの作成に使用されます。
  • BinaryWriter を使用すると、特定のエンコーディングで文字列を書き込むことができます。
  • BinaryWriter のオブジェクトを作成するには、BinaryWriter クラスのコンストラクターに Stream のオブジェクトを渡す必要があります。
  • BinaryWriter のオブジェクトの作成中にエンコードを指定しない場合、デフォルトで UTF-8 エンコードが使用されます。

説明付きの構文

BinaryWriter のオブジェクトを作成するコンストラクターは、4 つのオーバーロード形式で使用できます。すべてのオーバーロードされたコンストラクターを使用して 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);
  • 上記のステートメントは 2 番目と 3 番目のステートメントと同様に機能しますが、BinaryWriter オブジェクトが破棄された後に出力ストリームを開いたままにするかどうかを示すために使用できるデータ型 Boolean の追加パラメーターがある点が異なります。
  • ストリームを開いたままにするには、ブールパラメータの値を「true」に設定する必要があります。それ以外の場合は、「false」に設定する必要があります。
  • 「using」ブロック内に BinaryWriter クラスのオブジェクトを作成すると、オブジェクトの作業が完了して不要になったときに、オブジェクトによって占有されていたメモリが自動的に解放されます。

コード:

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

ここで、File.Open() メソッドは、BinaryWriter のインスタンスの作成に役立つ FileStream オブジェクトを返します。

BinaryWriter は C# でどのように動作しますか?

  • C# では、BinaryWriter はバイナリ データをファイルに書き込むために使用されます。または、バイナリ ファイルの作成に使用されるとも言えます。これは、プリミティブ データ型をバイナリ形式でストリームに書き込むのに役立ちます。また、特定の文字エンコーディングで文字列を記述するのにも役立ちます。
  • BinaryWriter を使用するには、プログラムに System.IO 名前空間をインポートする必要があります。次に、「new」演算子を使用し、Stream オブジェクトを BinaryWriter のコンストラクターにバイパスすることで、BinaryWriter クラスのオブジェクトを作成できます。
  • BinaryWriter のインスタンスを作成するには、通常、Stream オブジェクトをコンストラクターに提供すると同時に、ファイルの書き込み中に使用するエンコーディングを指定するオプションのパラメーターを提供します。ユーザーが文字エンコーディングを指定しない場合は、デフォルトで UTF-8 エンコーディングが使用されます。
  • BinaryWriter のオブジェクトの作成中にコンストラクターに渡すことができる別のオプションのパラメーターがあります。このパラメータはブール型で、BinaryWriter オブジェクトが破棄された後、ユーザーが現在のストリームを開いたままにするかどうかを指定するために使用されます。
  • BinaryWriter クラスは、さまざまな種類のデータに対してさまざまな Write() メソッドを提供します。これらのメソッドは、バイナリ ファイルにデータを書き込むために使用されます。 Write(Int32) メソッドを使用して、4 バイトの符号付き整数を現在のストリームに書き込み、ストリームの位置を 4 バイト進めます。

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# の BinaryWriter

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# の BinaryWriterの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。