Textreader和TextWriter是分别读取和写入文件的另一种方法,即使它们不是流类也是如此。 StreamReader和StreamWriter类分别从TextReader和TextWriter类派生。
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim line As String
Dim readFile As System.IO.TextReader = New _
StreamReader("C:\TextReader.txt")
While True
line = readFile.ReadLine()
If line Is Nothing Then
Exit While
Else
MsgBox(line)
End If
End While
readFile.Close()
readFile = Nothing
Catch ex As IOException
MsgBox(ex.ToString)
End Try
End Sub
End Class
执行此程序时,TextReader逐行读取文件。