Showing posts with label ReadLine. Show all posts
Showing posts with label ReadLine. Show all posts

Wednesday, November 3, 2010

Use the Peek method of the StreamReader

HOW TO: Use the Peek method
Syntax
streamReaderVariableName.Peek

Example
Dim lineOfText As String
   Do Until inFile.Peek = -1
   lineOfText = inFile.ReadLine
   MessageBox.Show(lineOfText)
Loop
reads each line of the file associated with the inFile variable, line by line. Each line (excluding the newline character) is assigned to the lineOfText variable and is then displayed in a messagebox.

Reading data from a Sequential Access File

HOW TO: Declare a StreamReader Variable
Syntax
{Dim | Private} streamReaderVariableName As IO.StreamReader

Example
Dim inFile As IO.StreamReader
declares a StreamReader variable named inFile.

HOW TO: Create a StreamReader Object
Syntax
IO.File.OpenText(fileName)

Example
inFile = IO.File.OpenText(“report.txt”)
opens the report.txt file for input, creates a StreamReader object and assigns it to the inFile variable.

HOW TO: Read data from a sequential access file
Syntax
streamReaderVariableName.ReadLine

Example
Dim message As String
message = inFile.ReadLine
reads a line of text from the file associated with the inFile variable and assigns the line, excluding the newline character, to the message variable.