C# 中的二进制编写器

WBOY
发布: 2024-09-03 15:22:35
原创
333 人浏览过

在 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中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!