StreamReader

StreamReader


Reads characters from a byte array in a particular encoding.


Remarks

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.

Example

' 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 Sub

See Also

Project VBCorLib Overview | Constructors | StreamWriter | TextReader

Implements:

IObject 
TextReader 

Public:

Properties:

NameDescription
 BaseStream (get)Returns the base stream this reader is reading from. 
 CurrentEncoding (get)Returns the current encoding used by the reader. 

Methods:

NameDescription
 CloseReaderCloses the reader. 
 DiscardBufferedDataDiscards the currently buffered data to allow reading from a new position in the underlying stream. 
 EqualsReturns a boolean indicating if the value and this object instance are the same instance. 
 GetHashCodeReturns a pseudo-unique number identifying this instance. 
 PeekReturns the the next char from the reader without consuming it. 
 ReadReturns the next char from the reader. 
 ReadBlockReads a block of characters from the the stream. 
 ReadLineReturns a string of characters to the next new-line character. 
 ReadToEndReturns a string of all remaining characters in the stream. 
 ToStringReturns a string representation of this object instance.