VB.Net - 二进制文件
BinaryReader类
BinaryReader类用于从文件读取二进制数据。 BinaryReader对象通过将FileStream对象传递给其构造函数来创建。下表显示了BinaryReader类的一些常用方法。
S.N | 方法名称和用途 |
---|---|
1 | Public Overridable Sub Close It closes the BinaryReader object and the underlying stream. 它关闭BinaryReader对象和底层流。 |
2 | Public Overridable Function Read As Integer Reads the characters from the underlying stream and advances the current position of the stream. 从底层流读取字符,并提高流的当前位置。 |
3 | Public Overridable Function ReadBoolean As Boolean Reads a Boolean value from the current stream and advances the current position of the stream by one byte. 从当前流读取一个布尔值,并将流的当前位置前进一个字节。 |
4 | Public Overridable Function ReadByte As Byte Reads the next byte from the current stream and advances the current position of the stream by one byte. 从当前流读取下一个字节,并将流的当前位置前进一个字节。 |
5 | Public Overridable Function ReadBytes (count As Integer) As Byte() Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes. 从当前流读取指定数量的字节为字节数组,并将当前位置前移该字节数。 |
6 | Public Overridable Function ReadChar As Char Reads the next character from the current stream and advances the current position of the stream in accordance with the Encoding used and the specific character being read from the stream. 从当前流读取下一个字符,并根据使用的编码和从流中读取的特定字符提前流的当前位置。 |
7 | Public Overridable Function ReadChars (count As Integer) As Char() Reads the specified number of characters from the current stream, returns the data in a character array, and advances the current position in accordance with the Encoding used and the specific character being read from the stream. 从当前流读取指定数量的字符,返回字符数组中的数据,并根据使用的编码和从流中读取的特定字符提前当前位置。 |
8 | Public Overridable Function ReadDouble As Double Reads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes. 从当前流读取8字节浮点值,并将流的当前位置提前8个字节。 |
9 | Public Overridable Function ReadInt32 As Integer Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes. 从当前流读取4字节有符号整数,并将流的当前位置前移四个字节。 |
10 | Public Overridable Function ReadString As String Reads a string from the current stream. The string is prefixed with the length, encoded as an integer seven bits at a time. 从当前流读取字符串。 字符串以长度为前缀,一次编码为7位整数。 |
BinaryWriter类
BinaryWriter类用于将二进制数据写入流。 通过将FileStream对象传递给其构造函数来创建BinaryWriter对象。下表显示了BinaryWriter类的一些常用方法。
S.N | 函数名称和描述 |
---|---|
1 | Public Overridable Sub Close It closes the BinaryWriter object and the underlying stream. 它关闭BinaryWriter对象和底层流。 |
2 | Public Overridable Sub Flush Clears all buffers for the current writer and causes any buffered data to be written to the underlying device. 清除当前写入程序的所有缓冲区,并使任何缓冲的数据写入底层设备。 |
3 | Public Overridable Function Seek (offset As Integer, origin As SeekOrigin ) As Long Sets the position within the current stream. 设置当前流中的位置。 |
4 | Public Overridable Sub Write (value As Boolean) Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing true. 将一个字节的布尔值写入当前流,0表示false,1表示true。 |
5 | Public Overridable Sub Write (value As Byte) Writes an unsigned byte to the current stream and advances the stream position by one byte. 将无符号字节写入当前流,并将流位置前移一个字节。 |
6 | Public Overridable Sub Write (buffer As Byte()) Writes a byte array to the underlying stream. 将字节数组写入基础流。 |
7 | Public Overridable Sub Write (ch As Char ) Writes a Unicode character to the current stream and advances the current position of the stream in accordance with the Encoding used and the specific characters being written to the stream. 将Unicode字符写入当前流,并根据使用的编码和写入流的特定字符提前流的当前位置。 |
8 | Public Overridable Sub Write (chars As Char()) Writes a character array to the current stream and advances the current position of the stream in accordance with the Encoding used and the specific characters being written to the stream. 将字符数组写入当前流,并根据使用的编码和正在写入流的特定字符提前流的当前位置。 |
9 | Public Overridable Sub Write (value As Double ) Writes an eight-byte floating-point value to the current stream and advances the stream position by eight bytes. 将八字节浮点值写入当前流,并将流位置前移八个字节。 |
10 | Public Overridable Sub Write (value As Integer ) Writes a four-byte signed integer to the current stream and advances the stream position by four bytes. 将四字节有符号整数写入当前流,并将流位置前移四个字节。 |
11 | Public Overridable Sub Write (value As String ) Writes a length-prefixed string to this stream in the current encoding of the BinaryWriter and advances the current position of the stream in accordance with the encoding used and the specific characters being written to the stream. 在BinaryWriter的当前编码中向此流中写入一个以长度为前缀的字符串,并根据所使用的编码和正在写入流的特定字符来提前流的当前位置。 |
有关方法的完整列表,请访问Microsoft的文档。
示例:
以下示例演示读取和写入二进制数据:
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
更多建议: