VB.Net二進位檔

BinaryReaderBinaryWriter類用於讀取和寫入二進位檔。

BinaryReader類

BinaryReader類用於從檔中讀取二進位數據。BinaryReader對象是通過將FileStream對象傳遞給其構造函數來創建的。

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

編號 方法 描述
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 從當前流中讀取一個字串。該字串以長度為首碼,一次編碼為七位整數。

BinaryWriter類

BinaryWriter類用於將二進位數據寫入流。BinaryWriter對象是通過將FileStream對象傳遞給它的構造函數來創建的。

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

編號 方法 描述
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表示false1表示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的文檔。

示例

以下示例演示讀取和寫入二進位數據:


Imports System.IO
Module BinaryReadWrite
   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

執行上面示例代碼,得到以下結果 -

F:\worksp\vb.net\filehandle>vbc BinaryReadWrite.vb
Microsoft (R) Visual Basic Compiler version 14.0.1038
for Visual Basic 2012
Copyright (c) Microsoft Corporation.  All rights reserved.
......
F:\worksp\vb.net\filehandle>BinaryReadWrite.exe
Integer data: 25
Double data: 3.14157
Boolean data: True
String data: I am happy

上一篇: VB.Net檔處理 下一篇: VB.Net基本控件