Table of Contents

Class StreamReader

Namespace
System.IO
Assembly
System.IO.Streams.dll

Implements a TextReader that reads characters from a byte stream in a particular encoding.

public class StreamReader : TextReader, IDisposable
Inheritance
StreamReader
Implements
Inherited Members
Extension Methods

Constructors

StreamReader(Stream)

Initializes a new instance of the StreamReader class for the specified stream.

public StreamReader(Stream stream)

Parameters

stream Stream

The stream to be read.

Exceptions

ArgumentNullException

stream is null.

ArgumentException

stream does not support reading.

Properties

BaseStream

Returns the underlying stream.

public virtual Stream BaseStream { get; }

Property Value

Stream

The underlying stream.

Remarks

You use this property to access the underlying stream. The StreamReader class buffers input from the underlying stream when you call one of the Read methods. If you manipulate the position of the underlying stream after reading data into the buffer, the position of the underlying stream might not match the position of the internal buffer. To reset the internal buffer, call the DiscardBufferedData method; however, this method slows performance and should be called only when absolutely necessary. The StreamReader constructors that have the detectEncodingFromByteOrderMarks parameter can change the encoding the first time you read from the StreamReader object.

CurrentEncoding

Gets the current character encoding that the current StreamReader object is using.

public virtual Encoding CurrentEncoding { get; }

Property Value

Encoding

The current character encoding used by the current reader. The value can be different after the first call to any Read() method of StreamReader, since encoding autodetection is not done until the first call to a Read() method.

EndOfStream

Gets a value that indicates whether the current stream position is at the end of the stream.

public bool EndOfStream { get; }

Property Value

bool

true if the current stream position is at the end of the stream; otherwise false.

Methods

Close()

Closes the StreamReader object and the underlying stream, and releases any system resources associated with the reader.

public override void Close()

Remarks

This method overrides the Close() method. This implementation of Close() calls the Dispose(bool) method, passing a true value. Following a call to Close(), any operations on the reader might raise exceptions.

Dispose(bool)

Closes the underlying stream, releases the unmanaged resources used by the StreamReader, and optionally releases the managed resources.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Remarks

This method is called by the public Dispose(bool) method and the Finalize method. Dispose invokes the protected Dispose(bool) method with the disposing parameter set to true. Finalize invokes Dispose(bool) with disposing set to false. When the disposing parameter is true, this method releases all resources held by any managed objects that the StreamReader object references.This method invokes the Dispose(bool) method of each referenced object.

Peek()

Returns the next available character but does not consume it.

public override int Peek()

Returns

int

An integer representing the next character to be read, or -1 if there are no characters to be read or if the stream does not support seeking.

Remarks

The Peek() method returns an integer value in order to determine whether the end of the file, or another error has occurred. This allows a user to first check if the returned value is -1 before casting it to a char type. This method overrides Peek(). The current position of the StreamReader object is not changed by Peek().

Exceptions

IOException

An I/O error occurs.

Read()

Reads the next character from the input stream and advances the character position by one character.

public override int Read()

Returns

int

The next character from the input stream represented as an int object, or -1 if no more characters are available.

Remarks

This method overrides Read(). This method returns an integer so that it can return -1 if the end of the stream has been reached. If you manipulate the position of the underlying stream after reading data into the buffer, the position of the underlying stream might not match the position of the internal buffer.To reset the internal buffer, call the DiscardBufferedData method; however, this method slows performance and should be called only when absolutely necessary.

Read(char[], int, int)

Reads a specified maximum of characters from the current stream into a buffer, beginning at the specified index.

public override int Read(char[] buffer, int index, int count)

Parameters

buffer char[]

When this method returns, contains the specified character array with the values between index and (index + count - 1) replaced by the characters read from the current source.

index int

The index of buffer at which to begin writing.

count int

The maximum number of characters to read.

Returns

int

The number of characters that have been read, or 0 if at the end of the stream and no data was read. The number will be less than or equal to the count parameter, depending on whether the data is available within the stream.

Remarks

This method overrides TextReader.Read. This method returns an integer so that it can return 0 if the end of the stream has been reached. When using the Read method, it is more efficient to use a buffer that is the same size as the internal buffer of the stream, where the internal buffer is set to your desired block size, and to always read less than the block size.If the size of the internal buffer was unspecified when the stream was constructed, its default size is 4 kilobytes(4096 bytes). If you manipulate the position of the underlying stream after reading data into the buffer, the position of the underlying stream might not match the position of the internal buffer.To reset the internal buffer, call the DiscardBufferedData method; however, this method slows performance and should be called only when absolutely necessary. This method returns after either the number of characters specified by the count parameter are read, or the end of the file is reached. ReadBlock(char[], int, int) is a blocking version of Read(char[], int, int).

Exceptions

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

index or count is negative.

ArgumentException

The buffer length minus index is less than count.

ObjectDisposedException

An I/O error occurs, such as the stream is closed.

ReadLine()

Reads a line of characters from the current stream and returns the data as a string.

public override string ReadLine()

Returns

string

The next line from the input stream, or null if the end of the input stream is reached.

Exceptions

Exception

ReadToEnd()

Reads all characters from the current position to the end of the stream.

public override string ReadToEnd()

Returns

string

The rest of the stream as a string, from the current position to the end. If the current position is at the end of the stream, returns an empty string ("").

Remarks

This method overrides TextReader.ReadToEnd. ReadToEnd works best when you need to read all the input from the current position to the end of the stream.If more control is needed over how many characters are read from the stream, use the Read(Char[], Int32, Int32) method overload, which generally results in better performance. ReadToEnd assumes that the stream knows when it has reached an end.For interactive protocols in which the server sends data only when you ask for it and does not close the connection, ReadToEnd might block indefinitely because it does not reach an end, and should be avoided. Note that when using the Read method, it is more efficient to use a buffer that is the same size as the internal buffer of the stream.If the size of the buffer was unspecified when the stream was constructed, its default size is 4 kilobytes (4096 bytes). If the current method throws an OutOfMemoryException, the reader's position in the underlying Stream object is advanced by the number of characters the method was able to read, but the characters already read into the internal ReadLine buffer are discarded. If you manipulate the position of the underlying stream after reading data into the buffer, the position of the underlying stream might not match the position of the internal buffer. To reset the internal buffer, call the DiscardBufferedData method; however, this method slows performance and should be called only when absolutely necessary.