在 C# 中,BinaryWriter 是一个用于将原始类型写入特定编码流中的二进制数据的类。它存在于 System.IO 命名空间下。
以下是有关 BinaryWriter 的一些要点:
创建 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);
代码:
using (BinaryWriter binaryWriter = new BinaryWriter(File.Open(fileName, FileMode.Create )) ) { //user code }
这里,File.Open() 方法返回 FileStream 对象,该对象有助于创建 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. |
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.
以上是C# 中的二进制编写器的详细内容。更多信息请关注PHP中文网其他相关文章!