本文整理汇总了VB.NET中System.IO.BinaryWriter.Write方法的典型用法代码示例。如果您正苦于以下问题:VB.NET BinaryWriter.Write方法的具体用法?VB.NET BinaryWriter.Write怎么用?VB.NET BinaryWriter.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.BinaryWriter
的用法示例。
在下文中一共展示了BinaryWriter.Write方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: 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
示例2: Module1
' 导入命名空间
Imports System.IO
Module Module1
Const fileName As String = "AppSettings.dat"
Sub Main()
WriteDefaultValues()
DisplayValues()
End Sub
Sub WriteDefaultValues()
Using writer As BinaryWriter = New BinaryWriter(File.Open(fileName, FileMode.Create))
writer.Write(1.25F)
writer.Write("c:\Temp")
writer.Write(10)
writer.Write(True)
End Using
End Sub
Sub DisplayValues()
Dim aspectRatio As Single
Dim tempDirectory As String
Dim autoSaveTime As Integer
Dim showStatusBar As Boolean
If (File.Exists(fileName)) Then
Using reader As BinaryReader = New BinaryReader(File.Open(fileName, FileMode.Open))
aspectRatio = reader.ReadSingle()
tempDirectory = reader.ReadString()
autoSaveTime = reader.ReadInt32()
showStatusBar = reader.ReadBoolean()
End Using
Console.WriteLine("Aspect ratio set to: " & aspectRatio)
Console.WriteLine("Temp directory is: " & tempDirectory)
Console.WriteLine("Auto save time set to: " & autoSaveTime)
Console.WriteLine("Show status bar: " & showStatusBar)
End If
End Sub
End Module
示例3: 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) = binReader.ReadChar()
Next i
Console.WriteLine(memoryData)
End Sub
End Class
示例4: BinaryRW
' 导入命名空间
Imports System.IO
Public Class BinaryRW
Shared Sub Main()
Dim i As Integer
Const upperBound As Integer = 1000
' Create random data to write to the stream.
Dim dataArray(upperBound) As Double
Dim randomGenerator As New Random()
For i = 0 To upperBound
dataArray(i) = 100.1 * randomGenerator.NextDouble()
Next i
Dim binWriter As New BinaryWriter(New MemoryStream())
Try
' Write data to the stream.
Console.WriteLine("Writing data to the stream.")
For i = 0 To upperBound
binWriter.Write(dataArray(i))
Next i
' Create a reader using the stream from the writer.
Dim binReader As New BinaryReader(binWriter.BaseStream)
' Return to the beginning of the stream.
binReader.BaseStream.Position = 0
' Read and verify the data.
Try
Console.WriteLine("Verifying the written data.")
For i = 0 To upperBound
If binReader.ReadDouble() <> dataArray(i) Then
Console.WriteLine("Error writing data.")
Exit For
End If
Next i
Console.WriteLine("The data was written and verified.")
Catch ex As EndOfStreamException
Console.WriteLine("Error writing data: {0}.", _
ex.GetType().Name)
End Try
Finally
binWriter.Close()
End Try
End Sub
End Class
示例5: 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)
' 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())
Console.WriteLine(binReader.ReadChars( _
CInt(memStream.Length - memStream.Position)))
End Sub
End Class
示例6: 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
示例7: 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
示例8: BinaryRW
' 导入命名空间
Imports System.IO
Public Class BinaryRW
Shared Sub Main()
Dim i As Integer = 0
' Create random data to write to the stream.
Dim writeArray(1000) As Byte
Dim randomGenerator As New Random()
randomGenerator.NextBytes(writeArray)
Dim binWriter As New BinaryWriter(New MemoryStream())
Dim binReader As New BinaryReader(binWriter.BaseStream)
Try
' Write the data to the stream.
Console.WriteLine("Writing the data.")
For i = 0 To writeArray.Length - 1
binWriter.Write(writeArray(i))
Next i
' Set the stream position to the beginning of the stream.
binReader.BaseStream.Position = 0
' Read and verify the data from the stream.
For i = 0 To writeArray.Length - 1
If binReader.ReadByte() <> writeArray(i) Then
Console.WriteLine("Error writing the data.")
Return
End If
Next i
Console.WriteLine("The data was written and verified.")
' Catch the EndOfStreamException and write an error message.
Catch ex As EndOfStreamException
Console.WriteLine("Error writing the data: {0}", _
ex.GetType().Name)
End Try
End Sub
End Class
示例9: BinaryWriter.Write(Double d)
' 导入命名空间
Imports System.IO
Module Module1
Sub Main()
Dim Stream As FileStream
Try
Stream = New FileStream("test.dat", FileMode.Create)
Catch E As Exception
Console.WriteLine("Error creating test.Dat")
Console.WriteLine("Error {0}", E.Message)
End Try
Dim BinaryStream As New BinaryWriter(Stream)
Dim Age As Integer = 21
Dim Salary As Double = 100000.0
Dim Name As String = "Joe"
Try
BinaryStream.Write(Age)
BinaryStream.Write(Salary)
BinaryStream.Write(Name)
BinaryStream.Close()
Console.WriteLine("Data written to test.Dat")
Catch E As Exception
Console.WriteLine("Error writing to test.Dat")
Console.WriteLine("Error {0}", E.Message)
End Try
'Read
Try
Stream = New FileStream("test.dat", FileMode.Open)
Catch E As Exception
Console.WriteLine("Error opening test.Dat")
Console.WriteLine("Error {0}", E.Message)
End Try
Dim BinaryStreamReader As New BinaryReader(Stream)
Try
Age = BinaryStreamReader.ReadInt32()
Salary = BinaryStreamReader.ReadDouble()
Name = BinaryStreamReader.ReadString()
BinaryStreamReader.Close()
Console.WriteLine("Age: {0}", Age)
Console.WriteLine("Salary: {0}", Salary)
Console.WriteLine("Name: {0}", Name)
Catch E As Exception
Console.WriteLine("Error reading to test.Dat")
Console.WriteLine("Error {0}", E.Message)
End Try
End Sub
End Module