Read a file by using SeekOrigin : FileStream « Stream File « VB.Net Tutorial






Imports System.IO
Imports System.Text

        
Public Class Tester
    Public Shared Sub Main
    
        Dim myFileStream As FileStream
        Dim bteWrite() As Byte
        Dim intByte As Integer
        Dim lngLoop As Long
       
        Try
            intByte = Encoding.ASCII.GetBytes("asdf").Length
            ReDim bteWrite(intByte)
            bteWrite = Encoding.ASCII.GetBytes("asdf")
            myFileStream = File.OpenWrite("test.txt")

            Dim myOrigin As SeekOrigin
            Dim lngPosition As Long
    
            lngPosition = 123
            myOrigin = SeekOrigin.Begin
'           myOrigin = SeekOrigin.Current
'           myOrigin = SeekOrigin.End
            myFileStream.Seek(lngPosition, myOrigin)


            For lngLoop = 0 To intByte - 1
                myFileStream.Seek(1, SeekOrigin.Current)
                myFileStream.WriteByte(bteWrite(lngLoop))
            Next

            myFileStream.Close()

        Catch ex As IOException
            Console.WriteLine(ex.Message)
        End Try
    End Sub

End Class








13.6.FileStream
13.6.1.Write Bytes using FileStream
13.6.2.Read byte using FileStream
13.6.3.Read a file by using SeekOrigin
13.6.4.Read into a buffer
13.6.5.FileStream.Length
13.6.6.Use FileStream to write byte array
13.6.7.Convert String to ASCII byte code array and save
13.6.8.Convert String to Unicode byte code array and save
13.6.9.Read byte array and convert it to ASCII string
13.6.10.Read byte array and convert it to Unicode string
13.6.11.Save UTF8Encoding string to text file