TextReader.Read 方法
定义
从文本读取器读取数据。Reads data from the text reader.
重载
| Read() |
读取文本读取器中的下一个字符并使该字符的位置前移一个字符。Reads the next character from the text reader and advances the character position by one character. |
| Read(Span<Char>) |
从当前读取器中读取字符,并将数据写入指定的缓冲区。Reads the characters from the current reader and writes the data to the specified buffer. |
| Read(Char[], Int32, Int32) |
从当前读取器中读取指定数目的字符并从指定索引开始将该数据写入缓冲区。Reads a specified maximum number of characters from the current reader and writes the data to a buffer, beginning at the specified index. |
Read()
读取文本读取器中的下一个字符并使该字符的位置前移一个字符。Reads the next character from the text reader and advances the character position by one character.
public:
virtual int Read();
public virtual int Read ();
abstract member Read : unit -> int
override this.Read : unit -> int
Public Overridable Function Read () As Integer
返回
文本读取器中的下一个字符,或为 -1(如果没有更多可用字符)。The next character from the text reader, or -1 if no more characters are available. 默认实现将返回 -1。The default implementation returns -1.
例外
TextReader 是关闭的。The TextReader is closed.
出现 I/O 错误。An I/O error occurs.
注解
类是一个抽象类。The TextReader class is an abstract class. 因此,不要在代码中对其进行实例化。Therefore, you do not instantiate it in your code. 有关使用方法的示例 Read ,请参见 StreamReader.Read 方法。For an example of using the Read method, see the StreamReader.Read method.
另请参阅
- Read()
- Read()
- 文件和流 I/OFile and Stream I/O
- 如何:从文件中读取文本How to: Read Text from a File
- 如何:将文本写入文件How to: Write Text to a File
适用于
Read(Span<Char>)
从当前读取器中读取字符,并将数据写入指定的缓冲区。Reads the characters from the current reader and writes the data to the specified buffer.
public:
virtual int Read(Span<char> buffer);
public virtual int Read (Span<char> buffer);
abstract member Read : Span<char> -> int
override this.Read : Span<char> -> int
Public Overridable Function Read (buffer As Span(Of Char)) As Integer
参数
此方法返回时,包含指定的字符范围,这些字符由从当前源中读取的字符替换。When this method returns, contains the specified span of characters replaced by the characters read from the current source.
返回
已读取的字符数。The number of characters that have been read. 该数小于或等于 buffer 长度,具体取决于读取器中是否有可用的数据。The number will be less than or equal to the buffer length, depending on whether the data is available within the reader. 如果调用此方法时没有留下更多的字符供读取,则此方法返回 0(零)。This method returns 0 (zero) if it is called when no more characters are left to read.
例外
buffer 为 null。buffer is null.
从流中读取的字符数大于 buffer 的长度。The number of characters read from the stream is larger than the length of the buffer.
适用于
Read(Char[], Int32, Int32)
从当前读取器中读取指定数目的字符并从指定索引开始将该数据写入缓冲区。Reads a specified maximum number of characters from the current reader and writes the data to a buffer, beginning at the specified index.
public:
virtual int Read(cli::array <char> ^ buffer, int index, int count);
public virtual int Read (char[] buffer, int index, int count);
abstract member Read : char[] * int * int -> int
override this.Read : char[] * int * int -> int
Public Overridable Function Read (buffer As Char(), index As Integer, count As Integer) As Integer
参数
- buffer
- Char[]
当此方法返回时,包含指定的字符数组,此数组中 index 和 (index + count - 1) 之间的值被从当前源中读取的字符所替换。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
- Int32
在 buffer 中开始写入的位置。The position in buffer at which to begin writing.
- count
- Int32
最多读取的字符数。The maximum number of characters to read. 如果在将指定数量的字符读入缓冲区之前就已达读取器的末尾,则返回该方法。If the end of the reader is reached before the specified number of characters is read into the buffer, the method returns.
返回
已读取的字符数。The number of characters that have been read. 该数会小于或等于 count,具体取决于读取器中是否有可用的数据。The number will be less than or equal to count, depending on whether the data is available within the reader. 如果调用此方法时没有留下更多的字符供读取,则此方法返回 0(零)。This method returns 0 (zero) if it is called when no more characters are left to read.
例外
buffer 为 null。buffer is null.
缓冲区长度减去 index 小于 count。The buffer length minus index is less than count.
index 或 count 为负数。index or count is negative.
TextReader 是关闭的。The TextReader is closed.
出现 I/O 错误。An I/O error occurs.
注解
此方法在 count 读取或到达文件末尾后返回。This method returns after either count characters are read or the end of the file is reached. ReadBlock 是此方法的一个阻塞版本。ReadBlock is a blocking version of this method.
类是一个抽象类。The TextReader class is an abstract class. 因此,不要在代码中对其进行实例化。Therefore, you do not instantiate it in your code. 有关使用方法的示例 Read ,请参见 StreamReader.Read 方法。For an example of using the Read method, see the StreamReader.Read method.
另请参阅
- Read(Char[], Int32, Int32)
- Read(Char[], Int32, Int32)
- 文件和流 I/OFile and Stream I/O
- 如何:从文件中读取文本How to: Read Text from a File
- 如何:将文本写入文件How to: Write Text to a File