| StreamReader |
The StreamReader object cannot be created directly. In order to create a new StreamReader, use the Cor.NewStreamReader method.
Unlike the FileStream and MemoryStream classes, the StreamReader reads bytes that are of a particular encoding to be decoded. Instead of reading bytes, the StreamReader returns characters or strings of the decoded bytes.
The default encoding is UTF8Encoding.
' This example attempts to open a text file and'' read the lines and display them.Private Sub Main() Dim sr As StreamReader Dim line As String On Error GoTo errTrap '' Create a StreamReader to read in a file. Set sr = NewStreamReader("TestFile.txt") '' Read in each line and display it until '' the end of the file is reached. Do '' Read in characters until a carriage-return is reached. line = sr.ReadLine Console.WriteLine line '' A null string is equal to StrPtr(line) = 0 Loop While Not cString.IsNull(line) sr.CloseReader errTrap: Dim ex As Exception '' Check if an exception was thrown. If Catch(ex) Then Console.WriteLine "The file could not be opened." Console.WriteLine ex.Message End If '' Wait for the user to press enter. Console.ReadLineEnd SubSee Also
Project VBCorLib Overview | Constructors | StreamWriter | TextReader
| IObject | |
| TextReader |
| Name | Description |
|---|---|
BaseStream (get) | Returns the base stream this reader is reading from. |
CurrentEncoding (get) | Returns the current encoding used by the reader. |
| Name | Description |
|---|---|
CloseReader | Closes the reader. |
DiscardBufferedData | Discards the currently buffered data to allow reading from a new position in the underlying stream. |
Equals | Returns a boolean indicating if the value and this object instance are the same instance. |
GetHashCode | Returns a pseudo-unique number identifying this instance. |
Peek | Returns the the next char from the reader without consuming it. |
Read | Returns the next char from the reader. |
ReadBlock | Reads a block of characters from the the stream. |
ReadLine | Returns a string of characters to the next new-line character. |
ReadToEnd | Returns a string of all remaining characters in the stream. |
ToString | Returns a string representation of this object instance. |