BinaryWriter.Seek Method Sets the position within the current stream. : Binary File Write « File Directory « VB.Net






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

1.Creating a random file
2.Use BinaryWriter to store information into a binary file
3.Save Structure to a Binary File
4.Gets the underlying stream of the BinaryWriter.
5.BinaryWriter.Write Method Writes an unsigned byte to the current stream and advances the stream position by one byte.
6.BinaryWriter.Write(Byte[]) Writes a byte array to the underlying stream.
7.Writes a region of a byte array to the current stream.
8.Writes a Unicode character
9.Writes a character array
10.Writes a section of a character array
11.Writes a length-prefixed string