本文整理匯總了VB.NET中System.IO.BinaryReader.ReadBytes方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET BinaryReader.ReadBytes方法的具體用法?VB.NET BinaryReader.ReadBytes怎麽用?VB.NET BinaryReader.ReadBytes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.IO.BinaryReader
的用法示例。
在下文中一共展示了BinaryReader.ReadBytes方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: BinaryRW
' 導入命名空間
Imports System.IO
Public Class BinaryRW
Shared Sub Main()
Const upperBound As Integer = 1000
' Create random data to write to the stream.
Dim dataArray(upperBound) As Byte
Dim randomGenerator As New Random
randomGenerator.NextBytes(dataArray)
Dim binWriter As New BinaryWriter(New MemoryStream())
' Write the data to the stream.
Console.WriteLine("Writing the data.")
binWriter.Write(dataArray)
' Create the reader using the stream from the writer.
Dim binReader As New BinaryReader(binWriter.BaseStream)
' Set the stream position to the beginning of the stream.
binReader.BaseStream.Position = 0
' Read and verify the data.
Dim verifyArray() As Byte = _
binReader.ReadBytes(dataArray.Length)
If verifyArray.Length <> dataArray.Length Then
Console.WriteLine("Error writing the data.")
Return
End If
For i As Integer = 0 To upperBound
If verifyArray(i) <> dataArray(i) Then
Console.WriteLine("Error writing the data.")
Return
End If
Next i
Console.WriteLine("The data was written and verified.")
End Sub
End Class
示例2: Module1
' 導入命名空間
Imports System.IO
Imports System.Text
Module Module1
Private ReadOnly CHUNK_SIZE As Integer = 1024
Public Sub Main(args() As String)
If ((args.Length = 0) OrElse Not File.Exists(args(0))) Then
Console.WriteLine("Please provide an existing file name.")
Else
Using fs As FileStream = New FileStream(args(0), FileMode.Open, FileAccess.Read)
Using br As New BinaryReader(fs, New ASCIIEncoding())
Dim chunk(CHUNK_SIZE) As Byte
chunk = br.ReadBytes(CHUNK_SIZE)
While chunk.Length > 0
DumpBytes(chunk, chunk.Length)
chunk = br.ReadBytes(CHUNK_SIZE)
End While
End Using
End Using
End If
End Sub
Public Sub DumpBytes(bdata() As Byte, len As Integer)
Dim i As Integer
Dim j As Integer = 0
Dim dchar As Char
' 3 * 16 chars for hex display, 16 chars for text and 8 chars
' for the 'gutter' int the middle.
Dim dumptext As New StringBuilder(" ", 16 * 4 + 8)
For i = 0 To len - 1
dumptext.Insert(j * 3, String.Format("{0:X2} ", CType(bdata(i), Integer)))
dchar = Convert.ToChar(bdata(i))
' replace 'non-printable' chars with a '.'.
If Char.IsWhiteSpace(dchar) Or Char.IsControl(dchar) Then
dchar = "."
End If
dumptext.Append(dchar)
j += 1
If j = 16 Then
Console.WriteLine(dumptext)
dumptext.Length = 0
dumptext.Append(" ")
j = 0
End If
Next i
' display the remaining line
If j > 0 Then
' add blank hex spots to align the 'gutter'.
For i = j To 15
dumptext.Insert(j * 3, " ")
Next i
Console.WriteLine(dumptext)
End If
End Sub
End Module