The StreamReader class provides the necessary methods for reading from a text file and exposes methods that match those of the StreamWriter class (the Write and WriteLine methods). The StreamReader class’s constructor is overloaded. You can specify the FileStream object it will use to read data from the file, the encoding scheme, and the buffer size. The simplest form of the constructor is the following:
Dim SR As New StreamReader(FS)
Code language: PHP (php)
This declaration associates the SR variable with the file on which the FS FileStream object was created. This is the most common form of the StreamReader class’s constructor. To prepare your application for reading the contents of the file C:\My Documents\Meeting.txt, use the following statements:
Dim FS As FileStream
Dim SR As StreamReader
FS = New FileStream("c:\My Documents\Meeting.txt", _
System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write)
SR = New StreamReader(FS)
Code language: PHP (php)
You can also create a new StreamReader object directly on a file, with the following form of the constructor:
Dim SR As New StreamReader(path)
Code language: PHP (php)
With both forms of the constructor, you can specify the character encoding with a second argument, as well as a third argument that determines the size of the buffer to be used for the IO operations.
Methods
The StreamReader class provides the following methods for writing data to the underlying file.
Close
The Close method closes the current instance of the StreamReader class and releases any system resources associated with this object.
Peek
The Peek method returns the next character as an integer value, without actually removing it from the input stream. The Peek method doesn’t change the current position in the stream. If there are no more characters left in the stream, the value −1 is returned. The Peek method will also return −1 if the current stream doesn’t allow peeking.
Read
This method reads a number of characters from the StreamReader class to which it’s applied and returns the number of characters read. This value is usually the same as the number of characters you specified unless there aren’t as many characters in the file. If you have reached the end of the stream (which is the end of the file), the method returns the value −1. The syntax of the Read method is as follows, where count is the number of characters to be read, starting at the startIndex location in the file:
charsRead = SR.Read(chars, startIndex, count)
The characters are stored in the chars array of characters, starting at the index specified by the second argument. A simpler form of the Read method reads the next character from the stream and returns it as an integer value, where SR is a properly declared StreamReader class:
Dim newChar As Integer
newChar = SR.Read()
Code language: PHP (php)
ReadBlock
This method reads a number of characters from a text file and stores them in an array of characters. It accepts the same arguments as the Read method and returns the number of characters read.
Dim chars(count - 1) As Char
charsRead = SR.Read(chars, startIndex, count)
Code language: PHP (php)
ReadLine
This method reads the next line from the text file associated with the StreamReader class and returns a string. If you’re at the end of the file, the method returns the Null value. The syntax of the ReadLine method is the following:
Dim txtLine As String
txtLine = SR.ReadLine()
Code language: JavaScript (javascript)
A text line is a sequence of characters followed by a carriage return (\r), line feed (\n), or carriage return and line feed (\r\n). Notice that the NewLine character you might have specified for the specific file with the StreamWriter class is ignored by the ReadLine method. The string returned by the method doesn’t include the line terminator.
ReadToEnd
The last method for reading characters from a text file reads all the characters from the current position to the end of the file. We usually call this method once to read the entire file with a single statement and store its contents to a string variable. The syntax of the ReadToEnd method is as follows:
allText = SR.ReadToEnd()
To make sure you’re reading the entire file with the ReadToEnd method, reposition the file pointer at the beginning of the file with the Seek method of the underlying stream before calling the ReadToEnd method of the StreamReader class:
FS.Seek (0, SeekOrigin.Begin)
Code language: CSS (css)