Home and Learn: VB Net Course


Reading a Text File Line by Line

This lesson is part of an ongoing tutorial. The first part is here: How to open a Text File in VB .NET

Quite often, you don't want to read the whole file at once. You want to read it line by line. In which case, instead of using the ReadToEnd method, as we did in the previous section, you can use the ReadLine method:

The ReadLine method, as its name suggests, reads text one line at a time. In order to do this, though, you need to use a loop. You can then loop round each line and read it into a variable. Here's a coding example:

Using reader As New System.IO.StreamReader(FILE_NAME)

While Not reader.EndOfStream

Dim textLine As String = reader.ReadLine()

Textbox1.Text = TextLine & vbCrLf

End While

End Using

This time, we're using something called a using statement. Using ensures the file is closed even if parsing causes an error. So you don't need to close the reader yourself.

Next, we have a while loop. The Not reader.EndOfStream part means "loop while EndOfStream reports False". Inside the while loop, we read a line from the text file and place it into the text box.

Instead of placing the line directly into the texxt box, it is common to parse each line. If you have a function set up, you can then pass the line to that function.For example:

While Not reader.EndOfStream

Dim textLine As String = reader.ReadLine()

Dim returnValue = ParseLine(textLine)

Textbox1.Text = TextBox1.Text & returnValue & vbCrLf

End While

Once you have a line of text in your variable, though, it's up to you to parse it. For example, suppose the line of text coming in from the text file was this:

"UserName1, Password1, UserName2, Password2"

You would then have to chop the line down and do something which each segment. VB won't do this for you! (But you saw how to do this in the last section, when you used things like Split and Substring.)

But what you are doing in the loop is building up your variable with lines of text that are pulled from your text file. Once you have pulled all the text from your file, you can then put it into the text box. Here's our programme once more:

Dim FILE_PATH As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Dim FILE_NAME As String = "test.txt"
Dim FULL_PATH As String = IO.Path.Combine(FILE_PATH, FILE_NAME)

If System.IO.File.Exists(FULL_PATH) Then

Using reader As New System.IO.StreamReader(FULL_PATH)

While Not reader.EndOfStream

Dim textLine As String = reader.ReadLine()

Textbox1.Text = TextBox1.Text & TextLine & vbCrLf

End While

End Using

Else

MessageBox.Show("File does not exist.")

End If

So inside the loop, we go round building up the textLine variable. We then place it into Textbox1. The vbCrLf part gets you a carriage return and a new line. This replace vbNewLine, which is now deprecated.

 

In the next part, you'll learn how to write to a text file.

Back to the VB NET Contents Page

 


Email us: enquiry at homeandlearn.co.uk