[Serializable] |
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.
By default, a StreamReader is not thread safe. See TextReader.Synchronized for a thread-safe wrapper.
StreamReader.Read (char[], int, int) and StreamWriter.Write (char[], int, int) 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.
ctor #1 | Overloaded:.ctor(Stream stream) Initializes a new instance of the StreamReader class for the specified stream. |
ctor #2 | Overloaded:.ctor(string path) Initializes a new instance of the StreamReader class for the specified file name. |
ctor #3 | Overloaded:.ctor(Stream stream, bool detectEncodingFromByteOrderMarks) Initializes a new instance of the StreamReader class the specified stream, with the specified byte order mark detection option. |
ctor #4 | Overloaded:.ctor(Stream stream, Encoding encoding) Initializes a new instance of the StreamReader class for the specified stream with the specified character encoding. |
ctor #5 | Overloaded:.ctor(string path, bool detectEncodingFromByteOrderMarks) Initializes a new instance of the StreamReader class for the specified file name, with the specified byte order mark detection option. |
ctor #6 | Overloaded:.ctor(string path, Encoding encoding) Initializes a new instance of the StreamReader class for the specified file name and with the specified character encoding. |
ctor #7 | Overloaded:.ctor(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks) Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding and byte order mark detection option. |
ctor #8 | Overloaded:.ctor(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks) Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding and byte order mark detection option. |
ctor #9 | Overloaded:.ctor(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize) 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. |
ctor #10 | Overloaded:.ctor(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize) 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. |
Null | A StreamReader around an empty stream. |
BaseStream | Read-only Returns the underlying stream. |
CurrentEncoding | Read-only Gets the current character encoding that the current StreamReader is using. |
Close | Overridden: Closes the StreamReader and releases any system resources associated with the reader. |
CreateObjRef (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. |
DiscardBufferedData | Allows a StreamReader to discard its current data. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
InitializeLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.InitializeLifetimeService Obtains a lifetime service object to control the lifetime policy for this instance. |
Peek | Overridden: Returns the next available character but does not consume it. |
Read | Overloaded:Read() Overridden: Reads the next character from the input stream and advances the character position by one character. |
Read | Overloaded:Read(in char[] buffer, int index, int count) Overridden: Reads a maximum of count characters from the current stream into buffer, beginning at index. |
ReadBlock (inherited from System.IO.TextReader) |
See base class member description: System.IO.TextReader.ReadBlock Reads a maximum of count characters from the current stream and writes the data to buffer, beginning at index. |
ReadLine | Overridden: Reads a line of characters from the current stream and returns the data as a string. |
ReadToEnd | Overridden: Reads the stream from the current position to the end of the stream. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Dispose | Overridden: Releases the unmanaged resources used by the StreamReader and optionally releases the managed resources. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public StreamReader( |
stream
Exception Type | Condition |
---|---|
ArgumentException | stream does not support reading. |
ArgumentNullException | stream is null. |
private void getNewStreamReader() { //Get a new StreamReader in ascii format from a //file using a buffer and byte order mark detection StreamReader srAsciiFromFileFalse512 = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //file with byte order mark detection = false StreamReader srAsciiFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a file StreamReader srAsciiFromFile = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII); //Get a new StreamReader from a //file with byte order mark detection = false StreamReader srFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", false); //Get a new StreamReader from a file StreamReader srFromFile = new StreamReader("C:\\Temp\\Test.txt"); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false and a buffer StreamReader srAsciiFromStreamFalse512 = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false StreamReader srAsciiFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a FileStream StreamReader srAsciiFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII); //Get a new StreamReader from a //FileStream with byte order mark detection = false StreamReader srFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), false); //Get a new StreamReader from a FileStream StreamReader srFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt")); }
public StreamReader( |
path
Exception Type | Condition |
---|---|
ArgumentException | path is an empty string (""). |
ArgumentNullException | path is null. |
FileNotFoundException | The file cannot be found. |
DirectoryNotFoundException | The directory cannot be found. |
IOException | path includes an incorrect or invalid syntax for file name, directory name, or volume label. |
path can be a file name, including a file on a Universal Naming Convention (UNC) share.
path is not required to be a file stored on disk; it can be any part of a system that supports access via streams.
When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.private void getNewStreamReader() { //Get a new StreamReader in ascii format from a //file using a buffer and byte order mark detection StreamReader srAsciiFromFileFalse512 = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //file with byte order mark detection = false StreamReader srAsciiFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a file StreamReader srAsciiFromFile = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII); //Get a new StreamReader from a //file with byte order mark detection = false StreamReader srFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", false); //Get a new StreamReader from a file StreamReader srFromFile = new StreamReader("C:\\Temp\\Test.txt"); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false and a buffer StreamReader srAsciiFromStreamFalse512 = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false StreamReader srAsciiFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a FileStream StreamReader srAsciiFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII); //Get a new StreamReader from a //FileStream with byte order mark detection = false StreamReader srFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), false); //Get a new StreamReader from a FileStream StreamReader srFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt")); }
stream
detectEncodingFromByteOrderMarks
Exception Type | Condition |
---|---|
ArgumentException | stream does not support reading. |
ArgumentNullException | stream is null. |
The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first three bytes of the stream. It automatically recognizes UTF-8, little-endian Unicode, and big-endian Unicode text if the file starts with the appropriate byte order marks. See the Encoding.GetPreamble method for more information.
private void getNewStreamReader() { //Get a new StreamReader in ascii format from a //file using a buffer and byte order mark detection StreamReader srAsciiFromFileFalse512 = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //file with byte order mark detection = false StreamReader srAsciiFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a file StreamReader srAsciiFromFile = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII); //Get a new StreamReader from a //file with byte order mark detection = false StreamReader srFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", false); //Get a new StreamReader from a file StreamReader srFromFile = new StreamReader("C:\\Temp\\Test.txt"); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false and a buffer StreamReader srAsciiFromStreamFalse512 = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false StreamReader srAsciiFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a FileStream StreamReader srAsciiFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII); //Get a new StreamReader from a //FileStream with byte order mark detection = false StreamReader srFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), false); //Get a new StreamReader from a FileStream StreamReader srFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt")); }
stream
encoding
Exception Type | Condition |
---|---|
ArgumentException | stream does not support reading. |
ArgumentNullException | stream or encoding is null. |
private void getNewStreamReader() { //Get a new StreamReader in ascii format from a //file using a buffer and byte order mark detection StreamReader srAsciiFromFileFalse512 = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //file with byte order mark detection = false StreamReader srAsciiFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a file StreamReader srAsciiFromFile = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII); //Get a new StreamReader from a //file with byte order mark detection = false StreamReader srFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", false); //Get a new StreamReader from a file StreamReader srFromFile = new StreamReader("C:\\Temp\\Test.txt"); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false and a buffer StreamReader srAsciiFromStreamFalse512 = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false StreamReader srAsciiFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a FileStream StreamReader srAsciiFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII); //Get a new StreamReader from a //FileStream with byte order mark detection = false StreamReader srFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), false); //Get a new StreamReader from a FileStream StreamReader srFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt")); }
path
detectEncodingFromByteOrderMarks
Exception Type | Condition |
---|---|
ArgumentException | path is an empty string (""). |
ArgumentNullException | path is null. |
FileNotFoundException | The file cannot be found. |
DirectoryNotFoundException | The directory cannot be found. |
IOException | path includes an incorrect or invalid syntax for file name, directory name, or volume label. |
path can be a file name, including a file on a Universal Naming Convention (UNC) share.
path is not required to be a file stored on disk; it can be any part of a system that supports access via streams.
The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first three bytes of the stream. It automatically recognizes UTF-8, little-endian Unicode, and big-endian Unicode text if the file starts with the appropriate byte order marks. Otherwise, the user-provided encoding is used. See the Encoding.GetPreamble method for more information.
private void getNewStreamReader() { //Get a new StreamReader in ascii format from a //file using a buffer and byte order mark detection StreamReader srAsciiFromFileFalse512 = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //file with byte order mark detection = false StreamReader srAsciiFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a file StreamReader srAsciiFromFile = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII); //Get a new StreamReader from a //file with byte order mark detection = false StreamReader srFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", false); //Get a new StreamReader from a file StreamReader srFromFile = new StreamReader("C:\\Temp\\Test.txt"); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false and a buffer StreamReader srAsciiFromStreamFalse512 = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false StreamReader srAsciiFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a FileStream StreamReader srAsciiFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII); //Get a new StreamReader from a //FileStream with byte order mark detection = false StreamReader srFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), false); //Get a new StreamReader from a FileStream StreamReader srFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt")); }
path
encoding
Exception Type | Condition |
---|---|
ArgumentException | path is an empty string (""). |
ArgumentNullException | path or encoding is null. |
FileNotFoundException | The file cannot be found. |
DirectoryNotFoundException | The directory cannot be found. |
IOException | path includes an incorrect or invalid syntax for file name, directory name, or volume label. |
path can be a file name, including a file on a Universal Naming Convention (UNC) share.
path is not required to be a file stored on disk; it can be any part of a system that supports access via streams.
When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.private void getNewStreamReader() { //Get a new StreamReader in ascii format from a //file using a buffer and byte order mark detection StreamReader srAsciiFromFileFalse512 = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //file with byte order mark detection = false StreamReader srAsciiFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a file StreamReader srAsciiFromFile = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII); //Get a new StreamReader from a //file with byte order mark detection = false StreamReader srFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", false); //Get a new StreamReader from a file StreamReader srFromFile = new StreamReader("C:\\Temp\\Test.txt"); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false and a buffer StreamReader srAsciiFromStreamFalse512 = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false StreamReader srAsciiFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a FileStream StreamReader srAsciiFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII); //Get a new StreamReader from a //FileStream with byte order mark detection = false StreamReader srFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), false); //Get a new StreamReader from a FileStream StreamReader srFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt")); }
public StreamReader( |
stream
encoding
detectEncodingFromByteOrderMarks
Exception Type | Condition |
---|---|
ArgumentException | stream does not support reading. |
ArgumentNullException | stream or encoding is null. |
The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first three bytes of the stream. It automatically recognizes UTF-8, little-endian Unicode, and big-endian Unicode text if the file starts with the appropriate byte order marks. Otherwise, the user-provided encoding is used. See the Encoding.GetPreamble method for more information.
When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.private void getNewStreamReader() { //Get a new StreamReader in ascii format from a //file using a buffer and byte order mark detection StreamReader srAsciiFromFileFalse512 = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //file with byte order mark detection = false StreamReader srAsciiFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a file StreamReader srAsciiFromFile = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII); //Get a new StreamReader from a //file with byte order mark detection = false StreamReader srFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", false); //Get a new StreamReader from a file StreamReader srFromFile = new StreamReader("C:\\Temp\\Test.txt"); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false and a buffer StreamReader srAsciiFromStreamFalse512 = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false StreamReader srAsciiFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a FileStream StreamReader srAsciiFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII); //Get a new StreamReader from a //FileStream with byte order mark detection = false StreamReader srFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), false); //Get a new StreamReader from a FileStream StreamReader srFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt")); }
public StreamReader( |
path
encoding
detectEncodingFromByteOrderMarks
Exception Type | Condition |
---|---|
ArgumentException | path is an empty string (""). |
ArgumentNullException | path or encoding is null. |
FileNotFoundException | The file cannot be found. |
DirectoryNotFoundException | The directory cannot be found. |
IOException | path includes an incorrect or invalid syntax for file name, directory name, or volume label. |
The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first three bytes of the stream. It automatically recognizes UTF-8, little-endian Unicode, and big-endian Unicode text if the file starts with the appropriate byte order marks. Otherwise, the user-provided encoding is used. See the Encoding.GetPreamble method for more information.
path can be a file name, including a file on a Universal Naming Convention (UNC) share.
path is not required to be a file stored on disk; it can be any part of a system that supports access via streams.
When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.private void getNewStreamReader() { //Get a new StreamReader in ascii format from a //file using a buffer and byte order mark detection StreamReader srAsciiFromFileFalse512 = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //file with byte order mark detection = false StreamReader srAsciiFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a file StreamReader srAsciiFromFile = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII); //Get a new StreamReader from a //file with byte order mark detection = false StreamReader srFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", false); //Get a new StreamReader from a file StreamReader srFromFile = new StreamReader("C:\\Temp\\Test.txt"); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false and a buffer StreamReader srAsciiFromStreamFalse512 = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false StreamReader srAsciiFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a FileStream StreamReader srAsciiFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII); //Get a new StreamReader from a //FileStream with byte order mark detection = false StreamReader srFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), false); //Get a new StreamReader from a FileStream StreamReader srFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt")); }
public StreamReader( |
stream
encoding
detectEncodingFromByteOrderMarks
bufferSize
Exception Type | Condition |
---|---|
ArgumentException | The stream does not support reading. |
ArgumentNullException | stream or encoding is null. |
ArgumentOutOfRangeException | bufferSize is less than or equal to zero. |
This constructor allows you to change the encoding the first time you read from the StreamReader. The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first three bytes of the stream. It automatically recognizes UTF-8, little-endian Unicode, and big-endian Unicode text if the file starts with the appropriate byte order marks. Otherwise, the user-provided encoding is used. See the Encoding.GetPreamble method for more information.
private void getNewStreamReader() { //Get a new StreamReader in ascii format from a //file using a buffer and byte order mark detection StreamReader srAsciiFromFileFalse512 = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //file with byte order mark detection = false StreamReader srAsciiFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a file StreamReader srAsciiFromFile = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII); //Get a new StreamReader from a //file with byte order mark detection = false StreamReader srFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", false); //Get a new StreamReader from a file StreamReader srFromFile = new StreamReader("C:\\Temp\\Test.txt"); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false and a buffer StreamReader srAsciiFromStreamFalse512 = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false StreamReader srAsciiFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a FileStream StreamReader srAsciiFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII); //Get a new StreamReader from a //FileStream with byte order mark detection = false StreamReader srFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), false); //Get a new StreamReader from a FileStream StreamReader srFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt")); }
public StreamReader( |
path
encoding
detectEncodingFromByteOrderMarks
bufferSize
Exception Type | Condition |
---|---|
ArgumentException | path is an empty string (""). |
ArgumentNullException | path or encoding is null. |
FileNotFoundException | The file cannot be found. |
DirectoryNotFoundException | The directory cannot be found. |
IOException | path includes an incorrect or invalid syntax for file name, directory name, or volume label. |
ArgumentOutOfRangeException | buffersize is less than or equal to zero. |
This constructor allows you to change the encoding the first time you read from the StreamReader. The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first three bytes of the stream. It automatically recognizes UTF-8, little-endian Unicode, and big-endian Unicode text if the file starts with the appropriate byte order marks. Otherwise, the user-provided encoding is used. See the Encoding.GetPreamble method for more information.
The buffer size, in number of 16-bit characters, is set by bufferSize. If bufferSize is less than the minimum allowable size (128 characters), the minimum allowable size is used.
path can be a file name, including a file on a Universal Naming Convention (UNC) share.
path is not required to be a file stored on disk; it can be any part of a system that supports access via streams.
When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.private void getNewStreamReader() { //Get a new StreamReader in ascii format from a //file using a buffer and byte order mark detection StreamReader srAsciiFromFileFalse512 = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //file with byte order mark detection = false StreamReader srAsciiFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a file StreamReader srAsciiFromFile = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII); //Get a new StreamReader from a //file with byte order mark detection = false StreamReader srFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", false); //Get a new StreamReader from a file StreamReader srFromFile = new StreamReader("C:\\Temp\\Test.txt"); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false and a buffer StreamReader srAsciiFromStreamFalse512 = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ascii format from a //FileStream with byte order mark detection = false StreamReader srAsciiFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false); //Get a new StreamReader in ascii format from a FileStream StreamReader srAsciiFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII); //Get a new StreamReader from a //FileStream with byte order mark detection = false StreamReader srFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), false); //Get a new StreamReader from a FileStream StreamReader srFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt")); }
public static readonly StreamReader Null;
|
StreamReader srNull = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII); if(!srNull.Equals(StreamReader.Null)) { srNull.BaseStream.Seek(0, SeekOrigin.Begin); Console.WriteLine(srNull.ReadToEnd()); } srNull.Close();
public virtual Stream BaseStream {get;}
|
FileStream fs = new FileStream("log.txt", FileMode.OpenOrCreate, FileAccess.Read); // Create a Char reader. StreamReader w = new StreamReader(fs); // Set the StreamReader file pointer to the end. w.BaseStream.Seek(0, SeekOrigin.End);
public virtual Encoding CurrentEncoding {get;}
|
StreamReader srEncoding = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII); Console.WriteLine("Encoding: {0}", srEncoding.CurrentEncoding.EncodingName); srEncoding.Close();
public override void Close(); |
This implementation of Close calls the StreamReader.Dispose method passing a true value.
Flushing the stream will not flush its underlying encoder unless you explicitly call Close. Setting StreamWriter.AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.
Following a call to Close, any operations on the reader might raise exceptions.
requestedType
Exception Type | Condition |
---|---|
RemotingException | This instance is not a valid remoting object. |
public void DiscardBufferedData(); |
protected override void Dispose( |
disposing
This method calls the dispose method of the base class, Dispose.
~StreamReader(); |
public virtual int GetHashCode(); |
public object GetLifetimeService(); |
public Type GetType(); |
public virtual object InitializeLifetimeService(); |
public class MyClass : MarshalByRefObject { public override Object InitializeLifetimeService() { ILease lease = (ILease)base.InitializeLifetimeService(); if (lease.CurrentState == LeaseState.Initial) { lease.InitialLeaseTime = TimeSpan.FromMinutes(1); lease.SponsorshipTimeout = TimeSpan.FromMinutes(2); lease.RenewOnCallTime = TimeSpan.FromSeconds(2); } return lease; } }
protected object MemberwiseClone(); |
public override int Peek(); |
Exception Type | Condition |
---|---|
IOException | An I/O error occurs. |
The current position of the StreamReader is not changed by Peek. The returned value is -1 if no more characters are currently available.
StreamReader srPeek = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII); // set the file pointer to the beginning srPeek.BaseStream.Seek(0, SeekOrigin.Begin); // cycle while there is a next char while (srPeek.Peek() > -1) { Console.Write(srPeek.ReadLine()); } // close the reader and the file srPeek.Close();
public override int Read(); |
Exception Type | Condition |
---|---|
IOException | An I/O error occurs. |
Since attempting to read beyond the end of the stream is not an exceptional case, the return type of this method is an integer, allowing a return value of -1 that signifies reading beyond the end of the stream.
buffer
index
count
Exception Type | Condition |
---|---|
ArgumentException | The buffer length minus index is less than count. |
ArgumentNullException | buffer is null. |
ArgumentOutOfRangeException | index or count is negative. |
IOException | An I/O error occurs, such as the stream is closed. |
Since attempting to read beyond the end of the stream is not an exceptional case, the return type of this method is an integer, allowing a return value of -1 that signifies reading beyond the end of the stream.
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 internal buffer was unspecified when the stream was constructed, its default size is 4 kilobytes (4096 bytes).
This method returns after either count characters are read, or the end of the file is reached. TextReader.ReadBlock is a blocking version of StreamReader.Read.
StreamReader srRead = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII); // set the file pointer to the beginning srRead.BaseStream.Seek(0, SeekOrigin.Begin); srRead.BaseStream.Position = 0; while (srRead.BaseStream.Position < srRead.BaseStream.Length) { char[] buffer = new char[1]; srRead.Read(buffer, 0, 1); Console.Write(buffer[0].ToString()); srRead.BaseStream.Position++; } srRead.DiscardBufferedData(); srRead.Close();
buffer
index
count
Exception Type | Condition |
---|---|
ArgumentNullException | buffer is null. |
ArgumentException | The buffer length minus index is less than count. |
ArgumentOutOfRangeException | index or count is negative. |
IOException | An I/O error occurs. |
public override string ReadLine(); |
Exception Type | Condition |
---|---|
OutOfMemoryException | There is insufficient memory to allocate a buffer for the returned string. |
IOException | An I/O error occurs. |
This method overrides TextReader.ReadLine.
If the current method throws an OutOfMemoryException, the reader's position in the underlying Stream is advanced by the number of characters the method was able to read, but the characters already read into the internal StreamReader.ReadLine buffer are discarded. Since the position of the reader in the stream cannot be changed, the characters already read are unrecoverable, and can be accessed only by reinitializing the StreamReader. If the initial position within the stream is unknown or the stream does not support seeking, the underlying Stream also needs to be reinitialized.
To avoid such a situation and produce robust code you should use the StreamReader.Read method and store the read characters in a preallocated buffer.
StreamReader srReadLine = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII); srReadLine.BaseStream.Seek(0, SeekOrigin.Begin); while (srReadLine.Peek() > -1) { Console.WriteLine(srReadLine.ReadLine()); } srReadLine.Close();
public override string ReadToEnd(); |
Exception Type | Condition |
---|---|
OutOfMemoryException | There is insufficient memory to allocate a buffer for the returned string. |
IOException | An I/O error occurs. |
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 StreamReader.Read (char[], int, int), which generally results in better performance.
Note than 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 is advanced by the number of characters the method was able to read, but the characters already read into the internal StreamReader.ReadLine buffer are discarded. Since the position of the reader in the stream cannot be changed, the characters already read are unrecoverable, and can be accessed only by reinitializing the StreamReader. If the initial position within the stream is unknown or the stream does not support seeking, the underlying Stream also needs to be reinitialized.
To avoid such a situation and produce robust code you should use the StreamReader.Read method and store the read characters in a preallocated buffer.
StreamReader srReadToEnd = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII); srReadToEnd.BaseStream.Seek(0, SeekOrigin.Begin); Console.WriteLine(srReadToEnd.ReadToEnd()); srReadToEnd.Close();
public virtual string ToString(); |