TechNet
Export (0) Print
Expand All

StreamReader Class

 

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

To browse the .NET Framework source code for this type, see the Reference Source.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)

System::Object
  System::MarshalByRefObject
    System.IO::TextReader
      System.IO::StreamReader

[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class StreamReader : TextReader

NameDescription
System_CAPS_pubmethodStreamReader(Stream^)

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

System_CAPS_pubmethodStreamReader(Stream^, Boolean)

Initializes a new instance of the StreamReader class for the specified stream, with the specified byte order mark detection option.

System_CAPS_pubmethodStreamReader(Stream^, Encoding^)

Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding.

System_CAPS_pubmethodStreamReader(Stream^, Encoding^, Boolean)

Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding and byte order mark detection option.

System_CAPS_pubmethodStreamReader(Stream^, Encoding^, Boolean, Int32)

Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding, byte order mark detection option, and buffer size.

System_CAPS_pubmethodStreamReader(Stream^, Encoding^, Boolean, Int32, Boolean)

Initializes a new instance of the StreamReader class for the specified stream based on the specified character encoding, byte order mark detection option, and buffer size, and optionally leaves the stream open.

System_CAPS_pubmethodStreamReader(String^)

Initializes a new instance of the StreamReader class for the specified file name.

System_CAPS_pubmethodStreamReader(String^, Boolean)

Initializes a new instance of the StreamReader class for the specified file name, with the specified byte order mark detection option.

System_CAPS_pubmethodStreamReader(String^, Encoding^)

Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding.

System_CAPS_pubmethodStreamReader(String^, Encoding^, Boolean)

Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding and byte order mark detection option.

System_CAPS_pubmethodStreamReader(String^, Encoding^, Boolean, Int32)

Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding, byte order mark detection option, and buffer size.

NameDescription
System_CAPS_pubpropertyBaseStream

Returns the underlying stream.

System_CAPS_pubpropertyCurrentEncoding

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

System_CAPS_pubpropertyEndOfStream

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

NameDescription
System_CAPS_pubmethodClose()

Closes the StreamReader object and the underlying stream, and releases any system resources associated with the reader.(Overrides TextReader::Close().)

System_CAPS_pubmethodCreateObjRef(Type^)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodDiscardBufferedData()

Clears the internal buffer.

System_CAPS_pubmethodDispose()

Releases all resources used by the TextReader object.(Inherited from TextReader.)

System_CAPS_protmethodDispose(Boolean)

Closes the underlying stream, releases the unmanaged resources used by the StreamReader, and optionally releases the managed resources.(Overrides TextReader::Dispose(Boolean).)

System_CAPS_pubmethodEquals(Object^)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodPeek()

Returns the next available character but does not consume it.(Overrides TextReader::Peek().)

System_CAPS_pubmethodRead()

Reads the next character from the input stream and advances the character position by one character.(Overrides TextReader::Read().)

System_CAPS_pubmethodRead(array<Char>^, Int32, Int32)

Reads a specified maximum of characters from the current stream into a buffer, beginning at the specified index.(Overrides TextReader::Read(array<Char>^, Int32, Int32).)

System_CAPS_pubmethodReadAsync(array<Char>^, Int32, Int32)

Reads a specified maximum number of characters from the current stream asynchronously and writes the data to a buffer, beginning at the specified index. (Overrides TextReader::ReadAsync(array<Char>^, Int32, Int32).)

System_CAPS_pubmethodReadBlock(array<Char>^, Int32, Int32)

Reads a specified maximum number of characters from the current stream and writes the data to a buffer, beginning at the specified index.(Overrides TextReader::ReadBlock(array<Char>^, Int32, Int32).)

System_CAPS_pubmethodReadBlockAsync(array<Char>^, Int32, Int32)

Reads a specified maximum number of characters from the current stream asynchronously and writes the data to a buffer, beginning at the specified index.(Overrides TextReader::ReadBlockAsync(array<Char>^, Int32, Int32).)

System_CAPS_pubmethodReadLine()

Reads a line of characters from the current stream and returns the data as a string.(Overrides TextReader::ReadLine().)

System_CAPS_pubmethodReadLineAsync()

Reads a line of characters asynchronously from the current stream and returns the data as a string.(Overrides TextReader::ReadLineAsync().)

System_CAPS_pubmethodReadToEnd()

Reads all characters from the current position to the end of the stream.(Overrides TextReader::ReadToEnd().)

System_CAPS_pubmethodReadToEndAsync()

Reads all characters from the current position to the end of the stream asynchronously and returns them as one string.(Overrides TextReader::ReadToEndAsync().)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubfieldSystem_CAPS_staticNull

A StreamReader object around an empty stream.

System_CAPS_noteNote

To view the .NET Framework source code for this type, see the Reference Source. You can browse through the source code online, download the reference for offline viewing, and step through the sources (including patches and updates) during debugging; see instructions.

StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file.

System_CAPS_importantImportant

This type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the “Using an Object that Implements IDisposable” section in the IDisposable interface topic.

StreamReader defaults to UTF-8 encoding unless specified otherwise, instead of defaulting to the ANSI code page for the current system. UTF-8 handles Unicode characters correctly and provides consistent results on localized versions of the operating system. If you get the current character encoding using the CurrentEncoding property, the value is not reliable until after the first Read method, since encoding auto detection is not done until the first call to a Read method.

By default, a StreamReader is not thread safe. See TextReader::Synchronized for a thread-safe wrapper.

The Read(array<Char>^, Int32, Int32) and Write(array<Char>^, Int32, Int32) method overloads read and write the number of characters specified by the count parameter. These are to be distinguished from BufferedStream::Read and BufferedStream::Write, which read and write the number of bytes specified by the count parameter. Use the BufferedStream methods only for reading and writing an integral number of byte array elements.

System_CAPS_noteNote

When reading from a Stream, it is more efficient to use a buffer that is the same size as the internal buffer of the stream.

For a list of common I/O tasks, see Common I/O Tasks.

The following example uses an instance of StreamReader to read text from a file. The constructor used in this example is not supported for use in Windows Store Apps.

using namespace System;
using namespace System::IO;
int main()
{
   try
   {
      // Create an instance of StreamReader to read from a file.
      StreamReader^ sr = gcnew StreamReader( "TestFile.txt" );
      try
      {
         String^ line;

         // Read and display lines from the file until the end of 
         // the file is reached.
         while ( line = sr->ReadLine() )
         {
            Console::WriteLine( line );
         }
      }
      finally
      {
         if ( sr )
            delete (IDisposable^)sr;
      }
   }
   catch ( Exception^ e ) 
   {
      // Let the user know what went wrong.
      Console::WriteLine( "The file could not be read:" );
      Console::WriteLine( e->Message );
   }
}

The following example instantiates a StreamReader object and calls its ReadAsync method to read a file asynchronously.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft