课程表

VB.Net基本教程

VB.Net高级教程

工具箱
速查手册

VB.Net - 二进制文件

当前位置:免费教程 » 程序设计 » VB.Net
BinaryReader和BinaryWriter类用于读取和写入二进制文件。

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的文档。


示例:

以下示例演示读取和写入二进制数据:

  1. Imports System.IO
  2. Module fileProg
  3. Sub Main()
  4. Dim bw As BinaryWriter
  5. Dim br As BinaryReader
  6. Dim i As Integer = 25
  7. Dim d As Double = 3.14157
  8. Dim b As Boolean = True
  9. Dim s As String = "I am happy"
  10. 'create the file
  11. Try
  12. bw = New BinaryWriter(New FileStream("mydata", FileMode.Create))
  13. Catch e As IOException
  14. Console.WriteLine(e.Message + "\n Cannot create file.")
  15. Return
  16. End Try
  17. 'writing into the file
  18. Try
  19. bw.Write(i)
  20. bw.Write(d)
  21. bw.Write(b)
  22. bw.Write(s)
  23. Catch e As IOException
  24. Console.WriteLine(e.Message + "\n Cannot write to file.")
  25. Return
  26. End Try
  27. bw.Close()
  28. 'reading from the file
  29. Try
  30. br = New BinaryReader(New FileStream("mydata", FileMode.Open))
  31. Catch e As IOException
  32. Console.WriteLine(e.Message + "\n Cannot open file.")
  33. Return
  34. End Try
  35. Try
  36. i = br.ReadInt32()
  37. Console.WriteLine("Integer data: {0}", i)
  38. d = br.ReadDouble()
  39. Console.WriteLine("Double data: {0}", d)
  40. b = br.ReadBoolean()
  41. Console.WriteLine("Boolean data: {0}", b)
  42. s = br.ReadString()
  43. Console.WriteLine("String data: {0}", s)
  44. Catch e As IOException
  45. Console.WriteLine(e.Message + "\n Cannot read from file.")
  46. Return
  47. End Try
  48. br.Close()
  49. Console.ReadKey()
  50. End Sub
  51. End Module

当上述代码被编译和执行时,它产生以下结果:

  1. Integer data: 25
  2. Double data: 3.14157
  3. Boolean data: True
  4. String data: I am happy
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号