public abstract class TextReader : MarshalByRefObject, IDisposable
Object
MarshalByRefObject
TextReaderThis type implements IDisposable.
mscorlib
BCL
Represents an object that can read a sequential series of characters.
TextReader is designed for character input, whereas the StreamReader is designed for byte input and the StringReader class is designed for reading from a string.By default, a TextReader is not thread safe. For information on creating a thread-safe TextReader , see System.IO.TextReader.Synchronized(System.IO.TextReader) .
System.IO Namespace
TextReader Constructors
TextReader Methods
TextReader.Close Method
TextReader.Dispose Method
TextReader.Peek Method
TextReader.Read(char[], int, int) Method
TextReader.Read() Method
TextReader.ReadBlock Method
TextReader.ReadLine Method
TextReader.ReadToEnd Method
TextReader.Synchronized Method
TextReader.System.IDisposable.Dispose Method
TextReader Fields
protected TextReader();
Constructs a new instance of the TextReader class.
System.IO.TextReader Class, System.IO Namespace
public virtual void Close();
Closes the current TextReader instance and releases any system resources associated with it.
[Note: After a call to System.IO.TextReader.Close , any IO operation on the current instance might throw an exception.]
[Behaviors: This method is equivalent to System.IO.TextReader.Dispose(System.Boolean)(
true
).]
[Usage: Use this method to close the current instance and free any resources associated with it.]
System.IO.TextReader Class, System.IO Namespace
protected virtual void Dispose(bool disposing);
Releases the unmanaged resources used by the TextReader 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 TextReader references. This method invokes theDispose()
method of each referenced object.[Note: System.IO.TextReader.Dispose(System.Boolean) can be called multiple times by other objects. When overriding System.IO.TextReader.Dispose(System.Boolean)(Boolean), be careful not to reference objects that have been previously disposed in an earlier call to System.IO.TextReader.Dispose(System.Boolean) .]
System.IO.TextReader Class, System.IO Namespace
public virtual int Peek();
Reads the next character without changing the state of the reader or the character source.
The next character to be read, or -1 if no more characters are available.
Exception Type Condition IOException An I/O error has occurred.
The position of the TextReader in the source is not changed by this operation.[Behaviors: As described above.]
[Default: The default implementation returns -1.]
System.IO.TextReader Class, System.IO Namespace
public virtual int Read(char[] buffer, int index, int count);
Reads at most the specified number of characters from the current character source, and writes them to the provided character array.
- 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 source.
- index
- A Int32 that specifies the place in buffer at which to begin writing.
- count
- A Int32 that specifies the maximum number of characters to read. If the end of the stream is reached before count of characters is read into buffer , this method returns.
A Int32 containing the number of characters that were read, or zero if there were no more characters left to read. Can be less than count if the end of the stream has been reached.
Exception Type Condition ArgumentNullException buffer is null
.ArgumentException (index + count ) > buffer .
Length.ArgumentOutOfRangeException index < 0 - or-
count < 0.
IOException An I/O error occurred.
System.IO.TextReader.ReadBlock(System.Char[],System.Int32,System.Int32) is a blocking version of this method.[Behaviors: The provided character array can be changed only in the specified range.]
System.IO.TextReader Class, System.IO Namespace
public virtual int Read();
Reads the next character from the character source 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.
[Behaviors: As described above.]
[Default: The default implementation returns -1.]
System.IO.TextReader Class, System.IO Namespace
public virtual int ReadBlock(char[] buffer, int index, int count);
Reads a specified number of characters from the current stream into a provided character array.
- 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 source.
- index
- A Int32 that specifies the index in 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 were read, or zero if there were no more characters left to read. Can be less than count if the end of the stream has been reached.
Exception Type Condition ArgumentNullException buffer is null
.ArgumentException (index + count - 1) > buffer .
Length.ArgumentOutOfRangeException index < 0 - or-
count < 0.
IOException An I/O error occurred.
The method blocks until either the specified number of characters are read, or no more characters are available in the source.[Behaviors: As described above.]
System.IO.TextReader Class, System.IO Namespace
public virtual string ReadLine();
Reads a line of characters from the current character source.
A String containing the next line from the input stream, ornull
if all lines have been read. The returned string does not contain the line terminating character.
Exception Type Condition IOException An I/O error occurred. OutOfMemoryException There is insufficient memory to allocate a buffer for the returned string.
ArgumentOutOfRangeException The number of characters in the next line is larger than System.Int32.MaxValue.
A line is defined as a sequence of characters followed by a carriage return (0x000d), a line feed (0x000a), System.Environment.NewLine, or the end of stream marker.[Behaviors: As described above.]
System.IO.TextReader Class, System.IO Namespace
public virtual string ReadToEnd();
Reads all characters from the current position in the character source to the end of the source.
A string containing all characters from the current position to the end of the character source.
Exception Type Condition IOException An I/O error occurred. OutOfMemoryException There is insufficient memory to allocate a buffer for the returned string. ArgumentOutOfRangeException The number of characters from the current position to the end of the underlying stream is larger than System.Int32.MaxValue.
[Behaviors: As described above.]
System.IO.TextReader Class, System.IO Namespace
public static TextReader Synchronized(TextReader reader);
Creates a thread-safe wrapper around the specified TextReader instance.
- reader
- The TextReader to synchronize.
A thread-safe TextReader .
Exception Type Condition ArgumentNullException The reader parameter is null
.
This method returns a TextReader instance that wraps around the specified TextReader instance and restricts concurrent access to it by multiple threads.
System.IO.TextReader Class, System.IO Namespace
void IDisposable.Dispose();
Implemented to support the IDisposable interface. [Note: For more information, see System.IDisposable.Dispose.]
System.IO.TextReader Class, System.IO Namespace
public static readonly TextReader Null;
Provides a TextReader with no data to read from.
Reading from the System.IO.TextReader.Null text reader is similar to reading from the end of a stream:
- System.IO.TextReader.Read() and System.IO.TextReader.Peek methods return -1
- System.IO.TextReader.Read
(
Char[], Int32, Int32)
and System.IO.TextReader.ReadBlock(System.Char[],System.Int32,System.Int32) methods return zero- System.IO.TextReader.ReadLine and System.IO.TextReader.ReadToEnd methods return
null
.
System.IO.TextReader Class, System.IO Namespace