Vb.net text reader

By: Issac  

StreamReader and StreamWriter are actually derived from Textreader and TextWriter, they are another way to read and write to a file but they are not stream classes.

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

When you execute this program the TextReader read the file line by line.

The following code reads the entire content in a file into string using TextReader.

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:\Test1.txt")
line = readFile.ReadToEnd()
MsgBox(line)
readFile.Close()
readFile = Nothing
Catch ex As IOException
MsgBox(ex.ToString)
End Try
End Sub
End Class

When you execute this code, TextReader reads the entire file in one stretch.




Archived Comments


Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)

Comment on this tutorial