In C# ist BinaryWriter eine Klasse, die zum Schreiben primitiver Typen als Binärdaten in einen bestimmten Codierungsstream verwendet wird. Es ist im System.IO-Namespace vorhanden.
Im Folgenden finden Sie einige wichtige Punkte zu BinaryWriter:
Der Konstruktor zum Erstellen eines BinaryWriter-Objekts ist in vier überladenen Formen verfügbar. Die Syntax zum Erstellen eines BinaryWriter-Objekts unter Verwendung aller seiner überladenen Konstruktoren lautet wie folgt:
Syntax #1
protected BinaryWriter();
Es wird verwendet, um eine Instanz der BinaryWriter-Klasse zu initialisieren.
Syntax #2
BinaryWriter binaryWriter = new BinaryWriter(outputStream) ;
Die obige Anweisung initialisiert eine neue Instanz der BinaryWriter-Klasse auf der Grundlage des angegebenen Streams (outputStream) und unter Verwendung der UTF-8-Zeichenkodierung.
Syntax #3
BinaryWriter binaryWriter = new BinaryWriter(outputStream, encoding);
Die obige Anweisung initialisiert eine neue Instanz von BinaryWriter basierend auf dem angegebenen Stream (outputStream) und der Zeichencodierung (encoding).
Syntax #4
BinaryWriter binaryWriter = new BinaryWriter(outputStream, encoding, true);
Code:
using (BinaryWriter binaryWriter = new BinaryWriter(File.Open(fileName, FileMode.Create )) ) { //user code }
Hier gibt die File.Open()-Methode das FileStream-Objekt zurück, das beim Erstellen einer Instanz von BinaryWriter hilft.
Die folgende Tabelle zeigt Details einiger Write()-Methoden von BinaryWriter für verschiedene Datentypen:
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. |
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:
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.
Das obige ist der detaillierte Inhalt vonBinaryWriter in C#. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!