The example in the documentation demonstrates it pretty well.
This example creates a new file that it then reads in (if the file exists before creation it deletes the file and creates a new one).
-
Imports System
-
Imports System.IO
-
Imports System.Text
-
-
Public Class Test
-
-
Public Shared Sub Main()
-
Dim path As String = "c:\temp\MyTest.txt"
-
-
Try
-
If File.Exists(path) Then
-
File.Delete(path)
-
End If
-
-
Dim sw As StreamWriter = New StreamWriter(path)
-
sw.WriteLine("This")
-
sw.WriteLine("is some text")
-
sw.WriteLine("to test")
-
sw.WriteLine("Reading")
-
sw.Close()
-
-
Dim sr As StreamReader = New StreamReader(path)
-
-
Do While sr.Peek() >= 0
-
Console.WriteLine(sr.ReadLine())
-
Loop
-
sr.Close()
-
Catch e As Exception
-
Console.WriteLine("The process failed: {0}", e.ToString())
-
End Try
-
End Sub
-
End Class
The
Peek
method is used to check if the stream that contains the file has another line available for reading. If the
Peek
method returns a line that has a lenght greater than 0 it reads it in and displays it on the screen.
The try/catch block is there in case something goes wrong creating the file or reading the file.
-Frinny