StreamReader

StreamReader


Reads characters from a byte array in a particular encoding.


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.  
 EndOfStream (get) 

Methods:

NameDescription
 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.  

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.

Examples

This example attempts to open a text file and read the lines and display them.

Private Sub Main()
    Dim Reader As StreamReader
    Dim Line As String
    
    On Error GoTo Catch
    
    ' Create a StreamReader to read in a file.
    Set Reader = 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 = Reader.ReadLine
        Debug.Print Line
    Loop While Not CorString.IsNull(Line) ' A null string is equal to StrPtr(Line) = 0
    
    Reader.CloseReader
    Exit Sub
    
Catch:
    Dim Ex As Exception
    ' Check if an exception was thrown.
    If Catch(Ex) Then
        Debug.Print "The file could not be opened."
        Debug.Print Ex.Message
    End If
End Sub

See Also

Project CorLib Overview

Class StreamReader Overview

Constructors

StreamWriter

TextReader