C#IO中streamreader中文乱码没有关闭close()会怎样(不使用using的情况下)?

Type: System.IO.StreamReader
System.IO.StreamReader ClassAssembly: Mscorlib.dllNamespace: System.IO
Implements a
that reads characters from a byte stream in a particular encoding.
C# Syntax:
[Serializable]public class StreamReader : TextReader
StreamReader is designed for character input in a particular encoding, whereas the
class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file.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
for a thread-safe wrapper.
(char[], int, int) and
(char[], int, int) read and write the number of characters specified by the count parameter. These are to be distinguished from
, 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.Note
When reading from a
, it is more efficient to use a buffer that is the same size as the internal buffer of the stream.
System.IO.StreamReader Member List:
Public Constructors
Overloaded:.ctor(Stream stream) Initializes a new instance of the
class for the specified stream.
Overloaded:.ctor(string path) Initializes a new instance of the
class for the specified file name.
Overloaded:.ctor(Stream stream, bool detectEncodingFromByteOrderMarks) Initializes a new instance of the
class the specified stream, with the specified byte order mark detection option.
Overloaded:.ctor(Stream stream, Encoding encoding) Initializes a new instance of the
class for the specified stream with the specified character encoding.
Overloaded:.ctor(string path, bool detectEncodingFromByteOrderMarks) Initializes a new instance of the
class for the specified file name, with the specified byte order mark detection option.
Overloaded:.ctor(string path, Encoding encoding) Initializes a new instance of the
class for the specified file name and with the specified character encoding.
Overloaded:.ctor(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks) Initializes a new instance of the
class for the specified stream, with the specified character encoding and byte order mark detection option.
Overloaded:.ctor(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks) Initializes a new instance of the
class for the specified file name, with the specified character encoding and byte order mark detection option.
Overloaded:.ctor(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize) Initializes a new instance of the
class for the specified stream, with the specified character encoding, byte order mark detection option, and buffer size.
Overloaded:.ctor(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize) Initializes a new instance of the
class for the specified file name, with the specified character encoding, byte order mark detection option, and buffer size.
Public Fields
A StreamReader around an empty stream.
Public Properties
Read-only Returns the underlying stream.
Read-only Gets the current character encoding that the current StreamReader is using.
Public Methods
Overridden:
Closes the
and releases any system resources associated with the reader.
(inherited from System.MarshalByRefObject)
See base class member description:
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
Allows a StreamReader to discard its current data.
(inherited from System.Object)
See base class member description: Derived from System.Object, the primary base class for all objects.
(inherited from System.Object)
See base class member description: Derived from System.Object, the primary base class for all objects.
(inherited from System.MarshalByRefObject)
See base class member description:
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
(inherited from System.Object)
See base class member description: Derived from System.Object, the primary base class for all objects.
(inherited from System.MarshalByRefObject)
See base class member description:
Obtains a lifetime service object to control the lifetime policy for this instance.
Overridden:
Returns the next available character but does not consume it.
Overloaded:Read()Overridden:
Reads the next character from the input stream and advances the character position by one character.
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.
(inherited from System.IO.TextReader)
See base class member description:
Reads a maximum of count characters from the current stream and writes the data to buffer, beginning at index.
Overridden:
Reads a line of characters from the current stream and returns the data as a string.
Overridden:
Reads the stream from the current position to the end of the stream.
(inherited from System.Object)
See base class member description: Derived from System.Object, the primary base class for all objects.
Protected Methods
Overridden:
Releases the unmanaged resources used by the
and optionally releases the managed resources.
(inherited from System.Object)
See base class member description: Derived from System.Object, the primary base class for all objects.
(inherited from System.Object)
See base class member description: Derived from System.Object, the primary base class for all objects.
Hierarchy:
System.IO.StreamReader
System.IO.StreamReader Member DetailsOverloaded
Initializes a new instance of the
class for the specified stream.
C# Syntax:
public StreamReader(
Parameters:
The stream to be read.
Exceptions
Exception Type
stream does not support reading.
stream is null.
This constructor initializes the encoding to
property using the stream parameter, and the internal buffer to the default size. 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"),
//Get a new StreamReader from a FileStream
StreamReader srFromStream = new StreamReader(
(System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
Overloaded
Initializes a new instance of the
class for the specified file name.
C# Syntax:
public StreamReader(
Parameters:
The complete file path to be read.
Exceptions
Exception Type
path is an empty string ("").
path is null.
The file cannot be found.
The directory cannot be found.
path includes an incorrect or invalid syntax for file name, directory name, or volume label.
The complete file path is specified by path. The default character encoding and default buffer size are used.path can be a file name, including a file on a Universal Naming Convention (UNC) share.
path is not required to be a 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"),
//Get a new StreamReader from a FileStream
StreamReader srFromStream = new StreamReader(
(System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
Overloaded
Initializes a new instance of the
class the specified stream, with the specified byte order mark detection option.
C# Syntax:
public StreamReader(
detectEncodingFromByteOrderMarks);
Parameters:
The stream to be read.
detectEncodingFromByteOrderMarks
Indicates whether to look for byte order marks at the beginning of the file.
Exceptions
Exception Type
stream does not support reading.
stream is null.
This constructor initializes the encoding to
property using the stream parameter, and the internal buffer to the default size. 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
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"),
//Get a new StreamReader from a FileStream
StreamReader srFromStream = new StreamReader(
(System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
Overloaded
Initializes a new instance of the
class for the specified stream with the specified character encoding.
C# Syntax:
public StreamReader(
encoding);
Parameters:
The stream to be read.
The character encoding to use.
Exceptions
Exception Type
stream does not support reading.
stream or encoding is null.
The character encoding is set by encoding, and the default buffer size is used. 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"),
//Get a new StreamReader from a FileStream
StreamReader srFromStream = new StreamReader(
(System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
Overloaded
Initializes a new instance of the
class for the specified file name, with the specified byte order mark detection option.
C# Syntax:
public StreamReader(
detectEncodingFromByteOrderMarks);
Parameters:
The complete file path to be read.
detectEncodingFromByteOrderMarks
Indicates whether to look for byte order marks at the beginning of the file.
Exceptions
Exception Type
path is an empty string ("").
path is null.
The file cannot be found.
The directory cannot be found.
path includes an incorrect or invalid syntax for file name, directory name, or volume label.
This constructor initializes the encoding to
property using the stream parameter, and the internal buffer to the default size.path can be a file name, including a file on a Universal Naming Convention (UNC) share.
path is not required to be a 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
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"),
//Get a new StreamReader from a FileStream
StreamReader srFromStream = new StreamReader(
(System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
Overloaded
Initializes a new instance of the
class for the specified file name and with the specified character encoding.
C# Syntax:
public StreamReader(
encoding);
Parameters:
The complete file path to be read.
The character encoding to use.
Exceptions
Exception Type
path is an empty string ("").
path or encoding is null.
The file cannot be found.
The directory cannot be found.
path includes an incorrect or invalid syntax for file name, directory name, or volume label.
This constructor initializes the encoding as specified by the encoding parameter, and the internal buffer to the default size.path can be a file name, including a file on a Universal Naming Convention (UNC) share.
path is not required to be a 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"),
//Get a new StreamReader from a FileStream
StreamReader srFromStream = new StreamReader(
(System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
Overloaded
Initializes a new instance of the
class for the specified stream, with the specified character encoding and byte order mark detection option.
C# Syntax:
public StreamReader(
detectEncodingFromByteOrderMarks);
Parameters:
The stream to be read.
The character encoding to use.
detectEncodingFromByteOrderMarks
Indicates whether to look for byte order marks at the beginning of the file.
Exceptions
Exception Type
stream does not support reading.
stream or encoding is null.
This constructor initializes the encoding as specified by the encoding parameter, the
property using the stream parameter, and the internal buffer to the default size. 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
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"),
//Get a new StreamReader from a FileStream
StreamReader srFromStream = new StreamReader(
(System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
Overloaded
Initializes a new instance of the
class for the specified file name, with the specified character encoding and byte order mark detection option.
C# Syntax:
public StreamReader(
detectEncodingFromByteOrderMarks);
Parameters:
The complete file path to be read.
The character encoding to use.
detectEncodingFromByteOrderMarks
Indicates whether to look for byte order marks at the beginning of the file.
Exceptions
Exception Type
path is an empty string ("").
path or encoding is null.
The file cannot be found.
The directory cannot be found.
path includes an incorrect or invalid syntax for file name, directory name, or volume label.
This constructor initializes the encoding as specified by the encoding parameter, and the internal buffer to the default size. 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
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 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"),
//Get a new StreamReader from a FileStream
StreamReader srFromStream = new StreamReader(
(System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
Overloaded
Initializes a new instance of the
class for the specified stream, with the specified character encoding, byte order mark detection option, and buffer size.
C# Syntax:
public StreamReader(
detectEncodingFromByteOrderMarks,
bufferSize);
Parameters:
The stream to be read.
The character encoding to use.
detectEncodingFromByteOrderMarks
Indicates whether to look for byte order marks at the beginning of the file.
bufferSize
The minimum buffer size.
Exceptions
Exception Type
The stream does not support reading.
stream or encoding is null.
bufferSize is less than or equal to zero.
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. 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
method for more information.Note
When reading from a
, it is more efficient to use a buffer that is the same size as the internal buffer of the stream. 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"),
//Get a new StreamReader from a FileStream
StreamReader srFromStream = new StreamReader(
(System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
Overloaded
Initializes a new instance of the
class for the specified file name, with the specified character encoding, byte order mark detection option, and buffer size.
C# Syntax:
public StreamReader(
detectEncodingFromByteOrderMarks,
bufferSize);
Parameters:
The complete file path to be read.
The character encoding to use.
detectEncodingFromByteOrderMarks
Indicates whether to look for byte order marks at the beginning of the file.
bufferSize
The minimum buffer size, in number of 16-bit characters.
Exceptions
Exception Type
path is an empty string ("").
path or encoding is null.
The file cannot be found.
The directory cannot be found.
path includes an incorrect or invalid syntax for file name, directory name, or volume label.
buffersize is less than or equal to zero.
This constructor initializes the encoding as specified by the encoding parameter. 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
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 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"),
//Get a new StreamReader from a FileStream
StreamReader srFromStream = new StreamReader(
(System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
A StreamReader around an empty stream.
C# Syntax:
public static readonly
When read methods are invoked on StreamReader.Null, zero is always returned. When
is invoked on StreamReader.Null, null is returned.
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();
(read-only)
Returns the underlying stream.
C# Syntax:
public virtual
BaseStream {}
StreamReader might buffer input such that the position of the underlying stream will not match the StreamReader position. The StreamReader constructors with the detectEncodingFromByteOrderMarks parameter can change the encoding the first time you read from the StreamReader.
This example shows a use of BaseStream with
to set the file pointer of the underlying stream to the beginning.
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);
(read-only)
Gets the current character encoding that the current StreamReader is using.
C# Syntax:
public virtual
CurrentEncoding {}
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();
Overridden Method: ()
Closes the
and releases any system resources associated with the reader.
C# Syntax:
public override void Close();
This method overrides
. This implementation of Close calls the
method passing a true value.
Flushing the stream will not flush its underlying encoder unless you explicitly call Close. Setting
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)
InheritedSee base class member description:
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
C# Syntax:
public virtual
CreateObjRef(
requestedType);
Parameters:
requestedType
of the object that the new
will reference.
Return Value:
Information required to generate a proxy.
Exceptions
Exception Type
This instance is not a valid remoting object.
Method: ()
Allows a StreamReader to discard its current data.
C# Syntax:
public void DiscardBufferedData();
Because Read does not update the current position of the underlying stream, instances of classes such as StringReader return more characters than are actually in the stream. Use DiscardBufferedData to discard this excess data.
Overridden Method: (
disposing)
Releases the unmanaged resources used by the
and optionally releases the managed resources.
C# Syntax:
protected override void Dispose(
disposing);
Parameters:
true to release both managed and false to release only unmanaged resources.
When the disposing parameter is true, this method releases all resources held by any managed objects that this System.IO.StreamReader references. This method invokes the Dispose method of each referenced object.Notes to inheritors: Dispose may be called multiple times by other objects. When overriding Dispose, be careful not to reference objects that have been previously disposed in an earlier call to Dispose. This method calls the dispose method of the base class, Dispose.
InheritedSee base class member description:
C# Syntax:
public virtual
For more information on members inherited from System.Object click on the link above.
Method: ()
InheritedSee base class member description:
C# Syntax:
~StreamReader();
For more information on members inherited from System.Object click on the link above.
Method: ()
InheritedSee base class member description:
C# Syntax:
public virtual
GetHashCode();
For more information on members inherited from System.Object click on the link above.
Method: ()
InheritedSee base class member description:
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
C# Syntax:
GetLifetimeService();
Return Value:
An object of type
used to control the lifetime policy for this instance.
For more information about lifetime services, see the
Method: ()
InheritedSee base class member description:
C# Syntax:
GetType();
For more information on members inherited from System.Object click on the link above.
Method: ()
InheritedSee base class member description:
Obtains a lifetime service object to control the lifetime policy for this instance.
C# Syntax:
public virtual
InitializeLifetimeService();
Return Value:
An object of type
used to control the lifetime policy for this instance. This is the current lifetime service object for this in otherwise, a new lifetime service object initialized to the value of the
For more information about lifetime services, see the
The following code example demonstrates creating a lease.
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);
Method: ()
InheritedSee base class member description:
C# Syntax:
MemberwiseClone();
For more information on members inherited from System.Object click on the link above.
Overridden Method: ()
Returns the next available character but does not consume it.
C# Syntax:
public override
Return Value:
The next character to be read, or -1 if no more characters are available or the stream does not support seeking.
Exceptions
Exception Type
An I/O error occurs.
This method overrides
. 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();
Overloaded Method: ()
Reads the next character from the input stream and advances the character position by one character.
C# Syntax:
public override
Return Value:
The next character from the input stream represented as an
, or -1 if no more characters are available.
Exceptions
Exception Type
An I/O error occurs.
This method overrides
. 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.
Overloaded Method: (
char[] buffer,
Reads a maximum of count characters from the current stream into buffer, beginning at index.
C# Syntax:
public override
char[] buffer,
Parameters:
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.
The index of buffer at which to begin writing.
The maximum number of characters to read.
Return Value:
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 count, depending on whether the data is available within the stream.
Exceptions
Exception Type
The buffer length minus index is less than count.
buffer is null.
index or count is negative.
An I/O error occurs, such as the stream is closed.
This method overrides
. 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.
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();
char[] buffer,
InheritedSee base class member description:
Reads a maximum of count characters from the current stream and writes the data to buffer, beginning at index.
C# Syntax:
public virtual
ReadBlock(
char[] buffer,
Parameters:
When this method returns, contains the specified character array with the values between index and(index + count) replaced by the characters read from the current source.
The place in buffer
at which to begin writing.
The maximum number of characters to read.
Return Value:
The number of characters that have been read. The number will be less than or equal to count, depending on whether all input characters have been read.
Exceptions
Exception Type
buffer is null.
The buffer length minus index is less than count.
index or count is negative.
An I/O error occurs.
The method blocks until either count characters are read, or all characters have been read. This is a blocking version of
Overridden Method: ()
Reads a line of characters from the current stream and returns the data as a string.
C# Syntax:
public override
ReadLine();
Return Value:
The next line from the input stream, or null if the end of the input stream is reached.
Exceptions
Exception Type
There is insufficient memory to allocate a buffer for the returned string.
An I/O error occurs.
A line is defined as a sequence of characters followed by a line feed ("\n") or a carriage return immediately followed by a line feed ("\r\n"). The string that is returned does not contain the terminating carriage return or line feed. The returned value is null if the end of the input stream is reached. This method overrides
If the current method throws an
, the reader's position in the underlying
is advanced by the number of characters the method was able to read, but the characters already read into the internal
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
. If the initial position within the stream is unknown or the stream does not support seeking, the underlying
also needs to be reinitialized.
To avoid such a situation and produce robust code you should use the
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();
Overridden Method: ()
Reads the stream from the current position to the end of the stream.
C# Syntax:
public override
ReadToEnd();
Return Value:
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("").
Exceptions
Exception Type
There is insufficient memory to allocate a buffer for the returned string.
An I/O error occurs.
This method overrides
.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
(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
, the reader's position in the underlying
is advanced by the number of characters the method was able to read, but the characters already read into the internal
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
. If the initial position within the stream is unknown or the stream does not support seeking, the underlying
also needs to be reinitialized.
To avoid such a situation and produce robust code you should use the
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();
Method: ()
InheritedSee base class member description:
C# Syntax:
public virtual string ToString();
For more information on members inherited from System.Object click on the link above.}

我要回帖

更多关于 c streamreader 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信