本文整理匯總了VB.NET中System.IO.BinaryReader.Read方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET BinaryReader.Read方法的具體用法?VB.NET BinaryReader.Read怎麽用?VB.NET BinaryReader.Read使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.IO.BinaryReader
的用法示例。
在下文中一共展示了BinaryReader.Read方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: BinaryRW
' 導入命名空間
Imports System.IO
Public Class BinaryRW
Shared Sub Main()
Dim i As Integer = 0
Dim invalidPathChars() As Char = Path.InvalidPathChars
Dim memStream As new MemoryStream()
Dim binWriter As New BinaryWriter(memStream)
' Write to memory.
binWriter.Write("Invalid file path characters are: ")
For i = 0 To invalidPathChars.Length - 1
binWriter.Write(invalidPathChars(i))
Next i
' Create the reader using the same MemoryStream
' as used with the writer.
Dim binReader As New BinaryReader(memStream)
' Set Position to the beginning of the stream.
memStream.Position = 0
' Read the data from memory and write it to the console.
Console.Write(binReader.ReadString())
Dim memoryData( _
CInt(memStream.Length - memStream.Position) - 1) As Char
For i = 0 To memoryData.Length - 1
memoryData(i) = Convert.ToChar(binReader.Read())
Next i
Console.WriteLine(memoryData)
End Sub
End Class
示例2: Module1
' 導入命名空間
Imports System.IO
Module Module1
Sub Main()
Const upperBound As Integer = 1000
Dim dataArray(upperBound) As Byte
Dim verifyArray(upperBound) As Byte
Dim randomGenerator As New Random
randomGenerator.NextBytes(dataArray)
Using binWriter As New BinaryWriter(New MemoryStream())
Console.WriteLine("Writing the data.")
binWriter.Write(dataArray, 0, dataArray.Length)
Using binReader As New BinaryReader(binWriter.BaseStream)
binReader.BaseStream.Position = 0
If binReader.Read(verifyArray, 0, dataArray.Length) <> dataArray.Length Then
Console.WriteLine("Error writing the data.")
Return
End If
End Using
End Using
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 Module
示例3: 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
示例4: BinaryRW
' 導入命名空間
Imports System.IO
Public Class BinaryRW
Shared Sub Main()
Dim invalidPathChars() As Char = Path.InvalidPathChars
Dim memStream As new MemoryStream()
Dim binWriter As New BinaryWriter(memStream)
' Write to memory.
binWriter.Write("Invalid file path characters are: ")
binWriter.Write(Path.InvalidPathChars, 0, _
Path.InvalidPathChars.Length)
' Create the reader using the same MemoryStream
' as used with the writer.
Dim binReader As New BinaryReader(memStream)
' Set Position to the beginning of the stream.
memStream.Position = 0
' Read the data from memory and write it to the console.
Console.Write(binReader.ReadString())
Dim upperBound As Integer = _
CInt(memStream.Length - memStream.Position) - 1
Dim memoryData(upperBound) As Char
binReader.Read(memoryData, 0, upperBound)
Console.WriteLine(memoryData)
End Sub
End Class
示例5: Tester
' 導入命名空間
Imports System.IO
Public Class Tester
Public Shared Sub Main
Dim fs As System.IO.FileStream
Dim r As System.IO.BinaryReader
Dim buffer(100) As Char
Dim mylength As Long
fs = New System.IO.FileStream("test.txt", IO.FileMode.OpenOrCreate)
r = New System.IO.BinaryReader(fs)
mylength = fs.Length
If mylength > 100 Then
mylength = 100
End If
r.Read(buffer, 0, mylength)
Console.WriteLine(buffer)
r.Close()
fs.Close()
End Sub
End Class