在C#中,BinaryReader是一个用于处理二进制数据的类。它位于 System.IO 命名空间下。 BinaryReader 用于将原始数据类型读取为特定编码流中的二进制值。 BinaryReader 使用 Stream 对象,即为了创建 BinaryReader 对象,我们需要在其构造函数中传递 Stream 对象。 BinaryReader 类具有三个重载构造函数来处理二进制数据。默认情况下,BinaryReader 使用 UTF-8 编码来读取数据,直到我们在创建其对象时指定其他字符编码。
我们可以通过以下三种方式创建 BinaryReader 对象:
BinaryReader binary_reader = new BinaryReader(inputStream);
上面的语句根据指定的流(inputStream)使用UTF-8编码初始化BinaryReader的新实例。
BinaryReader binary_reader = new BinaryReader(inputStream, encoding);
该语句根据指定的流(inputStream)和encoding指定的编码来初始化BinaryReader的新实例。
BinaryReader binary_reader = new BinaryReader(inputStream, encoding, true);
该语句的工作方式与上面两条语句相同,但带有一个 Boolean 类型的额外参数,该参数用于指定用户是否希望在 BinaryReader 对象被释放后使流保持打开状态。此参数需要为“true”才能使流保持打开状态,否则需要为“false”。
除了这三种方式之外,我们还可以使用以下语句创建BinaryReader:
using(BinaryReader binary_reader = new BinaryReader(File.Open(file_path, FileMode.Open))) { //user code }
在上面的语句中,File.Open() 方法返回 FileStream 的对象,因此它有助于创建 BinaryReader 的对象。
在“using”块内创建对象的好处是,当对象的工作完成且不再需要时,它会释放该对象所持有的内存。
BinaryReader 用于读取二进制信息,即用于读取二进制文件中存储的数据。二进制文件以机器可以轻松理解的方式存储数据,但对于人类来说很难理解这些数据。为了帮助理解此类数据,使用了 BinaryReader。为了使用 BinaryReader,我们首先需要在代码中导入 System.IO 命名空间。之后,我们需要借助“new”运算符创建 BinaryReader 的实例,并绕过 BinaryReader 构造函数内的 Stream 对象。
在创建 BinaryReader 实例时,我们提供要读取的流,如果不指定编码,我们可以选择指定要使用的字符编码,默认情况下使用 UTF-8 编码。除此之外,我们还可以选择指定是否希望在 BinaryReader 对象被释放后打开流,如下面的语句所示。
BinaryReader binary_reader = new BinaryReader(inputStream, encoding, true);
然后借助BinaryReader针对不同数据类型提供的不同Read()方法,我们可以从文件中读取数据。
BinaryReader 有许多支持不同数据类型的 Read() 方法,它们用于从流中读取原始数据类型。例如 BinaryReader 的 ReadString() 方法用于读取下一个字节作为字符串值,并将流中的当前位置前进一个字节。
BinaryReader针对不同类型数据的Read()方法如下表:
Method | Description |
Read() | It is used to read characters from an underlying stream and it also advances the current position of the stream according to the Encoding used and the specific character being read from the stream. |
ReadBoolean() | It is used to read the Boolean value from the stream and it also advances the current position of the stream by one byte. |
ReadByte() | It is used to read the next byte from the current stream and it also advances the current position of the stream by one byte. |
ReadChar() | It is used to read the next character from the current stream and it also advances the current position of the stream according to the Encoding used and the specific character being read from the stream. |
ReadDecimal() | It is used to read the decimal value from the current stream and it also advances the current position of the stream by sixteen bytes. |
ReadDouble() | It is used to read an 8-byte floating-point value from the current stream and advances the current position of the stream by eight bytes. |
ReadInt32() | It is used to read a 4-byte signed integer from the current stream and also it advances the current position of the stream by four bytes. |
ReadString() | It is used to read a string from the current stream. |
Example of creating a file using BinaryWriter and reading it using BInaryReader.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace ConsoleApp4 { public class Program { string filePath = "E:\\Content\\binaryFile.dat"; public void WriteFile() { try { //checking if the file already exists if (File.Exists(filePath)) { File.Delete(filePath); } FileStream stream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite); //creating binary file using BinaryWriter using (BinaryWriter writer = new BinaryWriter(stream)) { writer.Write("This is string"); writer.Write(100.53); writer.Write(true); } } catch(Exception ex) { Console.WriteLine(ex.Message); } } public void ReadFile() { try { //creating an object of Stream FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //creating BinaryReader using Stream object using (BinaryReader reader = new BinaryReader(stream)) { //reading data using Read() methods of different data types Console.WriteLine("String Value : " + reader.ReadString()); Console.WriteLine("Double Value : " + reader.ReadDouble()); Console.WriteLine("Boolean Value : " + reader.ReadBoolean()); } } catch(Exception ex) { Console.WriteLine(ex.Message); } } } public class BinaryReaderDemo { static void Main(string[] args) { Program obj = new Program(); obj.WriteFile(); obj.ReadFile(); Console.ReadKey(); } } }
Output:
BinaryReader is used to read primitive data types as binary values in a specific encoding stream. If not defined explicitly, by default BinaryReader uses UTF-8 encoding to read data. Stream object needs to be passed inside the constructor of BinaryReader in order to create its instance.
以上是C# 二进制读取器的详细内容。更多信息请关注PHP中文网其他相关文章!