BinaryWriter.Seek Method Sets the position within the current stream.
Imports System
Imports System.IO
Imports System.Text
Public Class BinReadWrite
Public Shared Sub Main()
Dim testfile As String = "C:\testfile.bin"
Dim fs As FileStream = File.Create(testfile)
Dim utf8 As New UTF8Encoding()
Dim bw As New BinaryWriter(fs, utf8)
Dim pos As Integer
For pos = 0 to 127
bw.Write(CType(pos, Byte))
Next pos
bw.Seek(0, SeekOrigin.Begin)
For pos = 0 To 119 Step 8
bw.Seek(7, SeekOrigin.Current)
bw.Write(CType(255, Byte))
Next pos
fs.Seek(0, SeekOrigin.Begin)
Dim rawbytes(fs.Length) As Byte
fs.Read(rawbytes, 0, fs.Length)
Dim i As Integer = 0
For Each b As Byte In rawbytes
Console.Write("{0:d3} ", b)
Next b
fs.Close()
End Sub
End Class
Related examples in the same category