VB.Net - 二進(jìn)制文件

2018-12-16 18:35 更新
BinaryReader和BinaryWriter類用于讀取和寫入二進(jìn)制文件。

BinaryReader類

BinaryReader類用于從文件讀取二進(jìn)制數(shù)據(jù)。 BinaryReader對(duì)象通過將FileStream對(duì)象傳遞給其構(gòu)造函數(shù)來創(chuàng)建。

下表顯示了BinaryReader類的一些常用方法。

S.N方法名稱和用途
1

Public Overridable Sub Close

It closes the BinaryReader object and the underlying stream.

它關(guān)閉BinaryReader對(duì)象和底層流。

2

Public Overridable Function Read As Integer

Reads the characters from the underlying stream and advances the current position of the stream.

從底層流讀取字符,并提高流的當(dāng)前位置。

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.

從當(dāng)前流讀取一個(gè)布爾值,并將流的當(dāng)前位置前進(jìn)一個(gè)字節(jié)。

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.

從當(dāng)前流讀取下一個(gè)字節(jié),并將流的當(dāng)前位置前進(jìn)一個(gè)字節(jié)。

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.

從當(dāng)前流讀取指定數(shù)量的字節(jié)為字節(jié)數(shù)組,并將當(dāng)前位置前移該字節(jié)數(shù)。

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.

從當(dāng)前流讀取下一個(gè)字符,并根據(jù)使用的編碼和從流中讀取的特定字符提前流的當(dāng)前位置。

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.

從當(dāng)前流讀取指定數(shù)量的字符,返回字符數(shù)組中的數(shù)據(jù),并根據(jù)使用的編碼和從流中讀取的特定字符提前當(dāng)前位置。

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.

從當(dāng)前流讀取8字節(jié)浮點(diǎn)值,并將流的當(dāng)前位置提前8個(gè)字節(jié)。

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.

從當(dāng)前流讀取4字節(jié)有符號(hào)整數(shù),并將流的當(dāng)前位置前移四個(gè)字節(jié)。

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.

從當(dāng)前流讀取字符串。 字符串以長度為前綴,一次編碼為7位整數(shù)。


BinaryWriter

BinaryWriter類用于將二進(jìn)制數(shù)據(jù)寫入流。 通過將FileStream對(duì)象傳遞給其構(gòu)造函數(shù)來創(chuàng)建BinaryWriter對(duì)象。

下表顯示了BinaryWriter類的一些常用方法。

S.N函數(shù)名稱和描述
1

Public Overridable Sub Close

It closes the BinaryWriter object and the underlying stream.

它關(guān)閉BinaryWriter對(duì)象和底層流。

2

Public Overridable Sub Flush

Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

清除當(dāng)前寫入程序的所有緩沖區(qū),并使任何緩沖的數(shù)據(jù)寫入底層設(shè)備。

3

Public Overridable Function Seek (offset As Integer, origin As SeekOrigin ) As Long

Sets the position within the current stream.

設(shè)置當(dāng)前流中的位置。

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.

將一個(gè)字節(jié)的布爾值寫入當(dāng)前流,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.

將無符號(hào)字節(jié)寫入當(dāng)前流,并將流位置前移一個(gè)字節(jié)。

6

Public Overridable Sub Write (buffer As Byte())

Writes a byte array to the underlying stream.

將字節(jié)數(shù)組寫入基礎(chǔ)流。

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字符寫入當(dāng)前流,并根據(jù)使用的編碼和寫入流的特定字符提前流的當(dāng)前位置。

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.

將字符數(shù)組寫入當(dāng)前流,并根據(jù)使用的編碼和正在寫入流的特定字符提前流的當(dāng)前位置。

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.

將八字節(jié)浮點(diǎn)值寫入當(dāng)前流,并將流位置前移八個(gè)字節(jié)。

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.

將四字節(jié)有符號(hào)整數(shù)寫入當(dāng)前流,并將流位置前移四個(gè)字節(jié)。

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的當(dāng)前編碼中向此流中寫入一個(gè)以長度為前綴的字符串,并根據(jù)所使用的編碼和正在寫入流的特定字符來提前流的當(dāng)前位置。


有關(guān)方法的完整列表,請(qǐng)?jiān)L問Microsoft的文檔。


示例:

以下示例演示讀取和寫入二進(jìn)制數(shù)據(jù):

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

當(dāng)上述代碼被編譯和執(zhí)行時(shí),它產(chǎn)生以下結(jié)果:

Integer data: 25
Double data: 3.14157
Boolean data: True
String data: I am happy

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)