二進制檔處理
二進制檔處理,使用執行個體型別為System.IO.FileStream,初始化參數為New System.IO.FileStream(絕對路徑, 使用模式)。
使用模式描述該執行個體相對於路徑上的二進制檔案是什麼關係。如果參數值為IO.FileMode.Open,則表示該執行個體用來讀取該檔案資料。
讀取時使用函式ReadByte,每次讀取一個位元組。其中,傳回值為-1 時表示超出檔案範圍,也就是讀取已經完畢。屬性Length 用來描述該檔案位元組陣列的長度。
]Dim test As New System.IO.FileStream("C:\test.txt", IO.FileMode.Open)
]Dim testGet(0) As Byte
]For i1 As Integer = 0 To test.Length - 1 Step 1
]ReDim Preserve testGet(UBound(testGet) + 1)
]testGet(UBound(testGet)) = test.ReadByte
]Next
]test.Close //使用完畢記得用Close 函式釋放資源。
寫入時使用模式為IO.FileMode.Create。寫入時使用函式WriteByte 一個一個位元組寫入。
]Dim test As New System.IO.FileStream("C:\test.txt", IO.FileMode.Open)
]test.WriteByte(0)
]test.Close //使用完畢也要用Close 釋放資源喔!
文章標籤
全站熱搜
