VB.Net Tutorial/Stream File/Stream

Материал из VB Эксперт
Перейти к: навигация, поиск

Read from text file

Imports System.IO

Public Class Tester
    Public Shared Sub Main
        Dim f As System.IO.File
        Dim r As System.IO.Stream
        Dim mylength As Integer
        Dim i As Integer
        Dim mybuffer(100) As Byte
        r = f.OpenRead("test.txt")
        mylength = r.Read(mybuffer, 0, 100)
        For i = 0 To mylength
            Console.WriteLine(Chr(mybuffer(i)))
        Next
        r.Close()
    End Sub
End Class

Seek file position

Imports System.IO

Public Class Tester
    Public Shared Sub Main
        Dim f As System.IO.File
        Dim r As System.IO.Stream
        Dim mylength As Integer
        Dim i As Integer
        Dim s As Integer
        Dim mybuffer(100) As Byte
        r = f.OpenRead("test.txt")
        mylength = r.Read(mybuffer, 0, 100)
        For i = 0 To mylength
            r.Seek(i, IO.SeekOrigin.Begin)
            s = r.ReadByte()
            Console.WriteLine(Chr(s))
        Next
        r.Close()
    End Sub
End Class

Stream read operation

Imports System.IO

Public Class Tester
    Public Shared Sub Main
        Dim f As System.IO.File
        Dim r As System.IO.Stream
        Dim mylength As Integer
        Dim i As Integer
        Dim mybuffer(100) As Byte
        r = f.OpenRead("test.txt")
        mylength = r.Read(mybuffer, 0, 100)
        For i = 0 To mylength
            Console.WriteLine(Chr(mybuffer(i)))
        Next
        r.Close()
    End Sub
End Class

Stream seek operation

Imports System.IO

Public Class Tester
    Public Shared Sub Main
        Dim f As System.IO.File
        Dim r As System.IO.Stream
        Dim mylength As Integer
        Dim i As Integer
        Dim s As Integer
        Dim mybuffer(100) As Byte
        r = f.OpenRead("test.txt")
        mylength = r.Read(mybuffer, 0, 100)
        For i = 0 To mylength
            r.Seek(i, IO.SeekOrigin.Begin)
            s = r.ReadByte()
            Console.WriteLine(Chr(s))
        Next
        r.Close()
    End Sub
End Class