public class StreamReader : TextReader
Object
MarshalByRefObject
TextReader
StreamReaderThis type implements IDisposable.
mscorlib
BCL
Implements a Stream that reads characters from a byte stream in a particular encoding.
The StreamReader class is designed for character input in a particular Encoding, whereas subclasses of Stream are designed for byte input and output.[Note: 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.
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.
By default, a StreamReader is not thread safe. For a thread-safe wrapper, see System.IO.TextReader.Synchronized(System.IO.TextReader) .
]
System.IO Namespace
StreamReader Constructors
StreamReader(System.String, System.Text.Encoding, bool, int) Constructor
StreamReader(System.String, System.Text.Encoding, bool) Constructor
StreamReader(System.String, System.Text.Encoding) Constructor
StreamReader(System.String, bool) Constructor
StreamReader(System.String) Constructor
StreamReader(System.IO.Stream, System.Text.Encoding, bool, int) Constructor
StreamReader(System.IO.Stream, System.Text.Encoding, bool) Constructor
StreamReader(System.IO.Stream) Constructor
StreamReader(System.IO.Stream, bool) Constructor
StreamReader(System.IO.Stream, System.Text.Encoding) Constructor
StreamReader Methods
StreamReader.Close Method
StreamReader.DiscardBufferedData Method
StreamReader.Dispose Method
StreamReader.Peek Method
StreamReader.Read() Method
StreamReader.Read(char[], int, int) Method
StreamReader.ReadLine Method
StreamReader.ReadToEnd Method
StreamReader Properties
StreamReader.BaseStream Property
StreamReader.CurrentEncoding Property
public StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
Constructs and 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.
- path
- A String that specifies the complete file path to read.
- encoding
- A Encoding that specifies the character encoding to use.
- detectEncodingFromByteOrderMarks
- A Boolean value that indicates whether the new StreamReaderis required to look for byte order marks at the beginning of the stream. Specify
true
to enable detection of byte order marks; otherwise, specifyfalse
.- bufferSize
- A Int32 that specifies the minimum buffer size, in number of 16-bit characters. If less than the minimum allowable size (128 characters), the minimum allowable size is used.
Exception Type Condition IOException path is in an invalid format or contains invalid characters. DirectoryNotFoundException The directory information specified in path was not found. FileNotFoundException The file specified in path was not found. ArgumentException path is an empty string (""). ArgumentNullException path or encoding is null
.ArgumentOutOfRangeException buffersize is less than or equal to zero.
This constructor initializes the System.IO.StreamReader.CurrentEncoding property using encoding .If requested, the current constructor detects the encoding by examining the first three bytes of the stream. The constructor 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 System.Text.Encoding.GetPreamble method for more information.
[Note: path is not required to be a file stored on disk; it can be any part of a system that supports access via streams. For example, depending on the system, this class might be able to access a physical device.
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 information on the valid format and characters for path strings, see Path.
]
System.IO.StreamReader Class, System.IO Namespace
public StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks);
Constructs and initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding and byte order mark detection option.
- path
- A String that specifies the complete file path to read.
- encoding
- A Encoding that specifies the character encoding to use.
- detectEncodingFromByteOrderMarks
- A Boolean value that indicates whether the new StreamReaderis required to look for byte order marks at the beginning of the stream. Specify
true
to enable detection of byte order marks; otherwise, specifyfalse
.
Exception Type Condition IOException path is in an invalid format or contains invalid characters. DirectoryNotFoundException The directory information specified in path was not found. FileNotFoundException The file specified in path was not found. ArgumentException path is an empty string (""). ArgumentNullException path or encoding is null
.
This constructor initializes the System.IO.StreamReader.CurrentEncoding property using encoding , and the internal buffer to the default size. [Note: The default buffer size is implementation defined.]
If requested, the current constructor detects the encoding by examining the first three bytes of the stream. The constructor 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 System.Text.Encoding.GetPreamble method for more information.
[Note: path is not required to be a file stored on disk; it can be any part of a system that supports access via streams. For example, depending on the system, this class might be able to access a physical device.
For information on the valid format and characters for path strings, see Path.
]
System.IO.StreamReader Class, System.IO Namespace
public StreamReader(string path, Encoding encoding);
Constructs and initializes a new instance of the StreamReader class for the specified file name and with the specified character encoding.
- path
- A String that specifies the complete file path to read.
- encoding
- A Encoding that specifies the character encoding to use.
Exception Type Condition IOException path is in an invalid format or contains invalid characters. DirectoryNotFoundException The directory information specified in path was not found. FileNotFoundException The file specified in path was not found. ArgumentException path is an empty string (""). ArgumentNullException path or encoding is null
.
This constructor initializes the System.IO.StreamReader.CurrentEncoding property using encoding , and the internal buffer to the default size. [Note: The default buffer size is implementation defined.]
[Note: path is not required to be a file stored on disk; it can be any part of a system that supports access via streams. For example, depending on the system, this class might be able to access a physical device.
For information on the valid format and characters for path strings, see Path.
]
System.IO.StreamReader Class, System.IO Namespace
public StreamReader(string path, bool detectEncodingFromByteOrderMarks);
Constructs and initializes a new instance of the StreamReader class for the specified file name, with the specified byte order mark detection option.
- path
- A String that specifies the complete file path to read.
- detectEncodingFromByteOrderMarks
- A Boolean value that indicates whether the new StreamReader is required to look for byte order marks at the beginning of the stream. Specify
true
to enable detection of byte order marks; otherwise, specifyfalse
.
Exception Type Condition IOException path is in an invalid format or contains invalid characters. DirectoryNotFoundException The directory information specified in path was not found. FileNotFoundException The file specified in path was not found. ArgumentException path is an empty string (""). ArgumentNullException path is null
.
This constructor initializes the System.IO.StreamReader.CurrentEncoding property to UTF8Encoding , and the internal buffer to the default size. [Note: The default buffer size is implementation defined.]
If requested, the current constructor detects the encoding by examining the first three bytes of the stream. The constructor automatically recognizes UTF-8, little-endian Unicode, and big-endian Unicode text if the file starts with the appropriate byte order marks. Otherwise, UTF-8 encoding is used. See the System.Text.Encoding.GetPreamble method for more information.
[Note: path is not required to be a file stored on disk; it can be any part of a system that supports access via streams. For example, depending on the system, this class might be able to access a physical device.
For information on the valid format and characters for path strings, see Path.
]
System.IO.StreamReader Class, System.IO Namespace
public StreamReader(string path);
Constructs and initializes a new instance of the StreamReader class for the specified file name.
- path
- A String that specifies the complete file path to read.
Exception Type Condition IOException path is in an invalid format or contains invalid characters. DirectoryNotFoundException The directory information specified in path was not found. FileNotFoundException The file specified in path was not found. ArgumentException path is an empty string (""). ArgumentNullException path is null
.
This constructor initializes the System.IO.StreamReader.CurrentEncoding property to UTF8Encoding, and the internal buffer to the default size. [Note: The default buffer size is implementation defined.]
[Note: path is not required to be a file stored on disk; it can be any part of a system that supports access via streams. For example, depending on the system, this class might be able to access a physical device.
For information on the valid format and characters for path strings, see Path .
]
System.IO.StreamReader Class, System.IO Namespace
public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
Constructs and 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.
- stream
- The Stream to read.
- encoding
- A Encoding that specifies the character encoding to use.
- detectEncodingFromByteOrderMarks
- A Boolean value that indicates whether the new StreamReader is required to look for byte order marks at the beginning of the stream. Specify
true
to enable detection of byte order marks; otherwise, specifyfalse
.- bufferSize
- A Int32 that specifies the minimum buffer size, in number of 16-bit characters. If bufferSize is less than the minimum allowable size (128 characters), the minimum allowable size is used.
Exception Type Condition ArgumentException stream does not support reading. ArgumentNullException stream or encoding is null
.ArgumentOutOfRangeException bufferSize is less than or equal to zero.
This constructor initializes the System.IO.StreamReader.CurrentEncoding property using encoding parameter the System.IO.StreamReader.BaseStream property using stream .If requested, this constructor detects the encoding by examining the first three bytes of the stream. The constructor 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. For more information, see the System.Text.Encoding.GetPreamble method.
[Note: 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.]
System.IO.StreamReader Class, System.IO Namespace
public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks);
Constructs and initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding and byte order mark detection option.
- stream
- The Stream to read.
- encoding
- A Encoding that specifies the character encoding to use.
- detectEncodingFromByteOrderMarks
- A Boolean value that indicates whether the new StreamReader is required to look for byte order marks at the beginning of the stream. Specify
true
to enable detection of byte order marks; otherwise, specifyfalse
.
Exception Type Condition ArgumentException stream does not support reading. ArgumentNullException stream or encoding is null
.
This constructor initializes the System.IO.StreamReader.CurrentEncoding property using encoding , the System.IO.StreamReader.BaseStream property using stream , and the internal buffer to the default size. [Note: The default buffer size is implementation defined.]
If requested, this constructor detects the encoding by examining the first three bytes of stream . This constructor automatically recognizes UTF-8, little-endian Unicode, and big-endian Unicode text if the stream starts with the appropriate byte order marks. Otherwise, the user-provided encoding is used. See the System.Text.Encoding.GetPreamble method for more information.
System.IO.StreamReader Class, System.IO Namespace
public StreamReader(Stream stream);
Constructs and initializes a new instance of the StreamReader class for the specified stream.
- stream
- The Stream to read.
Exception Type Condition ArgumentException stream does not support reading. ArgumentNullException stream is null
.
This constructor initializes the System.IO.StreamReader.CurrentEncoding property to UTF8Encoding, the System.IO.StreamReader.BaseStream property using stream , and the internal buffer to the default size. [Note: The default buffer size is implementation dependent.]
System.IO.StreamReader Class, System.IO Namespace
public StreamReader(Stream stream, bool detectEncodingFromByteOrderMarks);
Constructs and initializes a new instance of the StreamReader class for the specified stream, with the specified byte order mark detection option.
- stream
- The stream to read.
- detectEncodingFromByteOrderMarks
- A Boolean value that indicates whether the new StreamReader is required to look for byte order marks at the beginning of the stream. Specify
true
to enable detection of byte order marks; otherwise, specifyfalse
.
Exception Type Condition ArgumentException stream does not support reading. ArgumentNullException stream is null
.
This constructor initializes the System.IO.StreamReader.CurrentEncoding property to UTF8Encoding , the System.IO.StreamReader.BaseStream property using stream , and the internal buffer to the default size. [Note: The default buffer size is implementation defined.]
If requested, the current constructor detects the encoding by examining the first three bytes of the stream. The constructor automatically recognizes UTF-8, little-endian Unicode, and big-endian Unicode text if the file starts with the appropriate byte order marks. Otherwise, UTF-8 encoding is used. For more information, see the System.Text.Encoding.GetPreamble method.
System.IO.StreamReader Class, System.IO Namespace
public StreamReader(Stream stream, Encoding encoding);
Constructs and initializes a new instance of the StreamReader class for the specified stream with the specified character encoding.
- stream
- The Stream to read.
- encoding
- A Encoding that specifies the character encoding to use.
Exception Type Condition ArgumentException stream does not support reading. ArgumentNullException stream or encoding is null
.
This constructor initializes the System.IO.StreamReader.CurrentEncoding property using encoding , the System.IO.StreamReader.BaseStream property using stream , and the internal buffer to the default size. [Note: The default buffer size is implementation defined.]
System.IO.StreamReader Class, System.IO Namespace
public override void Close();
Closes the current instance of StreamReader , releasing any system resources associated with it.
Following a call to this method, operations on the current instance might raise exceptions.[Note: This version of System.IO.StreamReader.Close is equivalent to System.IO.StreamReader.Dispose(System.Boolean)(
true
).This method overrides System.IO.TextReader.Close.
]
System.IO.StreamReader Class, System.IO Namespace
public void DiscardBufferedData();
Allows a StreamReader to discard its buffered data.
[Note: This method is useful when reading from a stream after seeking to a new position. If this method is not called and the internal buffer is not empty, a read attempt at the new location will first return data that is in the buffer before returning the text at the current position in the stream.]
System.IO.StreamReader Class, System.IO Namespace
protected override void Dispose(bool disposing);
Releases the unmanaged resources used by the StreamReader and optionally releases the managed resources.
- disposing
true
to release both managed and unmanaged resources;false
to release only unmanaged resources.
When the disposing parameter istrue
, this method releases all resources held by any managed objects that this StreamReader references. This method invokes theDispose()
method of each referenced object.[Note: System.IO.StreamReader.Dispose(System.Boolean) can be called multiple times by other objects. When overriding System.IO.StreamReader.Dispose(System.Boolean)(Boolean), be careful not to reference objects that have been previously disposed in an earlier call to System.IO.StreamReader.Dispose(System.Boolean) .
This method calls the dispose method of the base class, System.IO.TextReader.Dispose(System.Boolean)(disposing).
]
System.IO.StreamReader Class, System.IO Namespace
public override int Peek();
Returns the next character in the underlying stream without advancing the position of the StreamReader in the stream.
The next character from the character source as a Int32 , or -1 if at the end of the stream.
Exception Type Condition IOException An I/O error occurred.
[Note: This method returns -1 is when the end of the underlying stream is reached because a Unicode character can contain only values between hexadecimal 0x0000 to 0xFFFF (0 to 65535).This method overrides System.IO.TextReader.Peek.
]
System.IO.StreamReader Class, System.IO Namespace
public override int Read();
Reads the next character from the input stream and advances the character position by one character.
The next character from the character source represented as a Int32 , or -1 if at the end of the stream.
Exception Type Condition IOException An I/O error occurred.
[Note: This method returns -1 when the end of the underlying stream is reached because a Unicode character can contain only values between hexadecimal 0x0000 to 0xFFFF (0 to 65535).This method overrides System.IO.TextReader.Read.
]
System.IO.StreamReader Class, System.IO Namespace
public override int Read(char[] buffer, int index, int count);
Reads a maximum of count characters from the current stream into buffer, beginning at index.
- buffer
- A Char array. 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 instance.
- index
- A Int32 that specifies the index of buffer at which to begin writing.
- count
- A Int32 that specifies the maximum number of characters to read.
A Int32 containing the number of characters that have been read, or zero if there are no more characters left to read. Can be less than count if the end of the stream has been reached.
Exception Type Condition ArgumentException buffer .Length - index < count . ArgumentNullException buffer is null
.ArgumentOutOfRangeException index or count is negative. IOException An I/O error occurred. -or-
The current stream is closed.
[Note: This method returns after either count characters are read, or the end of the file is reached. System.IO.TextReader.ReadBlock(System.Char[],System.Int32,System.Int32) is a blocking version of System.IO.StreamReader.Read .This method overrides System.IO.TextReader.Read.
]
System.IO.StreamReader Class, System.IO Namespace
public override string ReadLine();
Reads a line of characters from the current stream and returns the data as a string.
A String containing the next line from the input stream, ornull
if the end of the input stream is reached.
Exception Type Condition IOException An I/O error occurred. OutOfMemoryException There is insufficient memory to allocate a buffer for the returned string.
[Note: This method defines a line as a sequence of characters followed by a carriage return (hexadecimal 0x000d), a line feed (hexadecimal 0x000a), or System.Environment.NewLine. The returned string does not contain the terminating character(s).This method overrides System.IO.TextReader.ReadLine.
]
System.IO.StreamReader Class, System.IO Namespace
public override string ReadToEnd();
Reads the stream from the current position to the end of the stream.
A Int32 containing 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 the empty string ("").
Exception Type Condition IOException An I/O error occurred. OutOfMemoryException There is insufficient memory to allocate a buffer for the returned string.
[Note: This method overrides System.IO.TextReader.ReadToEnd .]
System.IO.StreamReader Class, System.IO Namespace
public virtual Stream BaseStream { get; }
Gets the underlying stream.
The underlying Stream which the current StreamReader instance is reading.
This property is read-only.
System.IO.StreamReader Class, System.IO Namespace
public virtual Encoding CurrentEncoding { get; }
Gets the current character encoding that the current StreamReader is using.
The current Encoding used by the current reader.
This property is read-only.The value returned by this property might change after the first call to a System.IO.StreamReader.Read method if encoding auto detection was specified to the constructor for the current instance.
System.IO.StreamReader Class, System.IO Namespace