Reading from sequential access files

Study the note on data file terminology before this one.

I created a file called friends.txt using Notepad. The file contains 10 records each with only one field, the name of a friend:

George Washington
Martha Washington
Abraham Lincoln
Mary Lincoln
John F. Kennedy
Jacqueline Kennedy
Dwight D. Eisenhower
Mamie Eisenhower
Harry Truman
Bess Truman

Then I wrote a program to read and display the file one record at a time. The following event handler is executed when the user clicks the List friends.txt button:

Private Sub btnList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click

'  declare and create a new StreamReader
   Dim nameFile As System.IO.StreamReader
   nameFile = New System.IO.StreamReader(txtRead.text)

'  declare other variables
   Dim strIn As String ' a string variable used to hold the records as they are read
   txtOut.Clear()      ' clear the output area

'  loop to read and display the file
   Do Until nameFile.Peek = -1
      strIn = nameFile.ReadLine
      txtOut.AppendText(strIn & vbCrLf)
   Loop

   nameFile.Close()
   txtOut.AppendText("That's all, folks!")

End Sub
This event handler, which executes when the user clicks on the button labeled list the file, illustrates the use of a new class of object called StreamReader. StreanReaders are used to read a stream of text from a sequential file.

StreamReaders do not appear in the user interface. Since there is no StreamReader control to drag from the toolbox to the user interface at design time, we create a new instance of StreamReader at execute time in the first two lines of the event handler.

The first line declares an object variable called nameFile which will refer to the StreamReader we create. The second line of the event handler creates the new StreamReader. New is a special method for constructing new objects.

The heart of the program is a two instruction loop. Each iteration of the loop reads then displays one record from the file. The ReadLine method of the StreamReader returns a string with the next record in the file, and that is assigned to the variable strIn. The second line of the loop appends the record that was just placed in strIn to the output TextBox.

The loop continues until the last record has been read. After the loop terminates, the event handler closes the file since we are finished reading it and displays the message "That's all folks!"

The test for the end of the loop uses the Peek method of the StreamReader. When there are no more records in the file, Peek returns a value of -1.

The StreamReader has an internal record pointer. When a file is opened, its record pointer is initially set to just before the first record, so that it will be read by the first ReadLine method call. When a record is read, the record pointer automatically advances to the next one. This continues until there are no more records, and the loop terminates.

Note finally, that the definition of the StreamReader class is part of the System.IO name space, and we have designated that so the compiler could find it.


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.