- VB.Net 基礎教程
- VB.Net - 首頁
- VB.Net - 概述
- VB.Net - 環境設定
- VB.Net - 程式結構
- VB.Net - 基本語法
- VB.Net - 資料型別
- VB.Net - 變數
- VB.Net - 常量
- VB.Net - 修飾符
- VB.Net - 語句
- VB.Net - 指令
- VB.Net - 運算子
- VB.Net - 決策
- VB.Net - 迴圈
- VB.Net - 字串
- VB.Net - 日期和時間
- VB.Net - 陣列
- VB.Net - 集合
- VB.Net - 函式
- VB.Net - 子程式
- VB.Net - 類和物件
- VB.Net - 異常處理
- VB.Net - 檔案處理
- VB.Net - 基本控制元件
- VB.Net - 對話方塊
- VB.Net - 高階窗體
- VB.Net - 事件處理
- VB.Net 高階教程
- VB.Net - 正則表示式
- VB.Net - 資料庫訪問
- VB.Net - Excel表格
- VB.Net - 傳送郵件
- VB.Net - XML處理
- VB.Net - Web程式設計
- VB.Net 有用資源
- VB.Net - 快速指南
- VB.Net - 有用資源
- VB.Net - 討論
VB.Net - 二進位制檔案
BinaryReader 和 BinaryWriter 類用於讀取和寫入二進位制檔案。
BinaryReader 類
BinaryReader 類用於從檔案中讀取二進位制資料。透過向其建構函式傳遞一個 FileStream 物件來建立一個 BinaryReader 物件。
下表顯示了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 類用於將二進位制資料寫入流。透過向其建構函式傳遞一個 FileStream 物件來建立一個 BinaryWriter 物件。
下表顯示了 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 代表 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 ) 將一個八位元組浮點值寫入當前流,並將流位置前進八個位元組。 |
| 10 |
Public Overridable Sub Write (value As Integer ) 將一個四位元組有符號整數寫入當前流,並將流位置前進四個位元組。 |
| 11 |
Public Overridable Sub Write (value As String ) 以 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