读取和写入二进制文件(Reading from and Writing into Binary files)
BinaryReader和BinaryWriter类用于读取和写入二进制文件。
BinaryReader类
BinaryReader类用于从文件中读取二进制数据。 通过将FileStream对象传递给其构造函数来创建BinaryReader对象。
下表显示了BinaryReader类的一些常用methods 。
Sr.No. | 方法名称和目的 |
---|---|
1 | Public Overridable Sub Close 它关闭BinaryReader对象和底层流。 |
2 | Public Overridable Function Read As Integer 从基础流中读取字符并提升流的当前位置。 |
3 | Public Overridable Function ReadBoolean As Boolean 从当前流中读取布尔值,并将流的当前位置提前一个字节。 |
4 | Public Overridable Function ReadByte As Byte 从当前流中读取下一个字节,并将流的当前位置前进一个字节。 |
5 | Public Overridable Function ReadBytes (count As Integer) As Byte() 将当前流中指定的字节数读入字节数组,并将当前位置提前该字节数。 |
6 | Public Overridable Function ReadChar As Char 从当前流中读取下一个字符,并根据所使用的编码和从流中读取的特定字符来提前流的当前位置。 |
7 | Public Overridable Function ReadChars (count As Integer) As Char() 从当前流中读取指定数量的字符,返回字符数组中的数据,并根据使用的编码和从流中读取的特定字符推进当前位置。 |
8 | Public Overridable Function ReadDouble As Double 从当前流中读取一个8字节的浮点值,并将流的当前位置提前八个字节。 |
9 | Public Overridable Function ReadInt32 As Integer 从当前流中读取一个4字节有符号整数,并将流的当前位置提前四个字节。 |
10 | Public Overridable Function ReadString As String 从当前流中读取字符串。 该字符串以长度为前缀,一次编码为整数7位。 |
BinaryWriter类
BinaryWriter类用于将二进制数据写入流。 通过将FileStream对象传递给其构造函数来创建BinaryWriter对象。
下表显示了BinaryWriter类的一些常用方法。
Sr.No. | 功能名称和描述 |
---|---|
1 | Public Overridable Sub Close 它会关闭BinaryWriter对象和底层流。 |
2 | Public Overridable Sub Flush 清除当前写入器的所有缓冲区,并将所有缓冲的数据写入底层设备。 |
3 | Public Overridable Function Seek (offset As Integer, origin As SeekOrigin ) As Long 设置当前流中的位置。 |
4 | Public Overridable Sub Write (value As Boolean) 将一个字节的布尔值写入当前流,0表示false,1表示true。 |
5 | Public Overridable Sub Write (value As Byte) 将无符号字节写入当前流并使流位置前进一个字节。 |
6 | Public Overridable Sub Write (buffer As Byte()) 将字节数组写入基础流。 |
7 | Public Overridable Sub Write (ch As Char ) 将Unicode字符写入当前流,并根据使用的编码和写入流的特定字符推进流的当前位置。 |
8 | Public Overridable Sub Write (chars As Char()) 将字符数组写入当前流,并根据使用的编码和写入流的特定字符推进流的当前位置。 |
9 | Public Overridable Sub Write (value As Double ) 将一个8字节的浮点值写入当前流,并将流位置前移8个字节。 |
10 | Public Overridable Sub Write (value As Integer ) 将一个四字节有符号整数写入当前流,并将流位置前移四个字节。 |
11 | Public Overridable Sub Write (value As String ) 在BinaryWriter的当前编码中将长度前缀的字符串写入此流,并根据使用的编码和写入流的特定字符来提前流的当前位置。 |
有关方法的完整列表,请访问Microsoft的文档。
例子 (Example)
以下示例演示了读取和写入二进制数据 -
Imports System.IO
Module fileProg
Sub Main()
Dim bw As BinaryWriter
Dim br As BinaryReader
Dim i As Integer = 25
Dim d As Double = 3.14157
Dim b As Boolean = True
Dim s As String = "I am happy"
'create the file
Try
bw = New BinaryWriter(New FileStream("mydata", FileMode.Create))
Catch e As IOException
Console.WriteLine(e.Message + "\n Cannot create file.")
Return
End Try
'writing into the file
Try
bw.Write(i)
bw.Write(d)
bw.Write(b)
bw.Write(s)
Catch e As IOException
Console.WriteLine(e.Message + "\n Cannot write to file.")
Return
End Try
bw.Close()
'reading from the file
Try
br = New BinaryReader(New FileStream("mydata", FileMode.Open))
Catch e As IOException
Console.WriteLine(e.Message + "\n Cannot open file.")
Return
End Try
Try
i = br.ReadInt32()
Console.WriteLine("Integer data: {0}", i)
d = br.ReadDouble()
Console.WriteLine("Double data: {0}", d)
b = br.ReadBoolean()
Console.WriteLine("Boolean data: {0}", b)
s = br.ReadString()
Console.WriteLine("String data: {0}", s)
Catch e As IOException
Console.WriteLine(e.Message + "\n Cannot read from file.")
Return
End Try
br.Close()
Console.ReadKey()
End Sub
End Module
编译并执行上述代码时,会产生以下结果 -
Integer data: 25
Double data: 3.14157
Boolean data: True
String data: I am happy