TextReader 类

定义

表示可读取有序字符系列的读取器。Represents a reader that can read a sequential series of characters.

public ref class TextReader abstract : MarshalByRefObject, IDisposable
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public abstract class TextReader : MarshalByRefObject, IDisposable
type TextReader = class
    inherit MarshalByRefObject
    interface IDisposable
Public MustInherit Class TextReader
Inherits MarshalByRefObject
Implements IDisposable
继承
派生
属性
实现

示例

TextReader 类是抽象类。The TextReader class is an abstract class. 因此,不要在代码中对其进行实例化。Therefore, you do not instantiate it in your code. StreamReader 类派生自 TextReader,并提供成员的实现以从流中读取。The StreamReader class derives from TextReader and provides implementations of the members for reading from a stream. 下面的示例演示如何使用 StreamReader.ReadAsync(Char[], Int32, Int32) 方法读取文件中的所有字符。The following example shows how to read all the characters in a file by using the StreamReader.ReadAsync(Char[], Int32, Int32) method. 它在将字符添加到 StringBuilder 类的实例之前,检查每个字符是否为字母、数字或空格。It checks whether each character is a letter, digit, or white space before adding the character to an instance of the StringBuilder class.

using System;
using System.Windows;
using System.IO;
using System.Text;

namespace WpfApplication
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string filename = @"C:\Example\existingfile.txt";
            char[] result;
            StringBuilder builder = new StringBuilder();

            using (StreamReader reader = File.OpenText(filename))
            {
                result = new char[reader.BaseStream.Length];
                await reader.ReadAsync(result, 0, (int)reader.BaseStream.Length);
            }
           
            foreach (char c in result)
            {
                if (char.IsLetterOrDigit(c) || char.IsWhiteSpace(c))
                {
                    builder.Append(c);
                }
            }
            FileOutput.Text = builder.ToString();
        }
    }
}
Imports System.Text
Imports System.IO

Class MainWindow

    Private Async Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
        Dim filename As String = "C:\Example\existingfile.txt"
        Dim result() As Char
        Dim builder As StringBuilder = New StringBuilder()

        Using reader As StreamReader = File.OpenText(filename)
            ReDim result(reader.BaseStream.Length)
            Await reader.ReadAsync(result, 0, reader.BaseStream.Length)
        End Using

        For Each c As Char In result
            If (Char.IsLetterOrDigit(c) Or Char.IsWhiteSpace(c)) Then
                builder.Append(c)
            End If
            Next
        FileOutput.Text = builder.ToString()
    End Sub
End Class

注解

TextReaderStreamReaderStringReader的抽象基类,分别从流和字符串读取字符。TextReader is the abstract base class of StreamReader and StringReader, which read characters from streams and strings, respectively. 使用这些派生类打开文本文件以读取指定范围内的字符,或创建基于现有流的读取器。Use these derived classes to open a text file for reading a specified range of characters, or to create a reader based on an existing stream.

重要

此类型实现 IDisposable 接口。This type implements the IDisposable interface. 当你完成使用从此类型派生的任何类型时,应直接或间接释放它。When you have finished using any type that derives from this type, you should dispose of it either directly or indirectly. 若要直接释放类型,请在 Disposetry/ 块中调用其 catch 方法。To dispose of the type directly, call its Dispose method in a try/catch block. 若要间接释放类型,请使用 using(在 C# 中)或 Using(在 Visual Basic 中)等语言构造。To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). 有关详细信息,请参阅 IDisposable 接口主题中的 Dispose 和 "使用实现 IDisposable 的对象" 一节。For more information, see Dispose and the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

实施者说明

派生类必须至少实现 Peek()Read() 方法才能 TextReader提供有用的实例。A derived class must minimally implement the Peek() and Read() methods to make a useful instance of TextReader.

构造函数

TextReader()

初始化 TextReader 类的新实例。Initializes a new instance of the TextReader class.

字段

Null

提供一个无数据可供读取的 TextReaderProvides a TextReader with no data to read from.

方法

Close()

关闭 TextReader 并释放与该 TextReader 关联的所有系统资源。Closes the TextReader and releases any system resources associated with the TextReader.

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(继承自 MarshalByRefObject)
Dispose()

释放由 TextReader 对象使用的所有资源。Releases all resources used by the TextReader object.

Dispose(Boolean)

释放 TextReader 使用的非托管资源,并选择性地释放托管资源。Releases the unmanaged resources used by the TextReader and optionally releases the managed resources.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(继承自 MarshalByRefObject)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(继承自 MarshalByRefObject)
Peek()

读取下一个字符但不更改读取器的状态或字符源。Reads the next character without changing the state of the reader or the character source. 返回下一个可用字符,而实际上并不从读取器中读取此字符。Returns the next available character without actually reading it from the reader.

Read()

从文本读取器中读取下一个字符并将字符位置提前一个字符。Reads the next character from the text reader and advances the character position by one character.

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(Span<Char>)

从当前读取器中读取字符,并将数据写入指定的缓冲区。Reads the characters from the current reader and writes the data to the specified buffer.

ReadAsync(Char[], Int32, Int32)

异步从当前文本读取器中读取指定最大字符数并从指定索引开始将该数据写入缓冲区。Reads a specified maximum number of characters from the current text reader asynchronously and writes the data to a buffer, beginning at the specified index.

ReadAsync(Memory<Char>, CancellationToken)

将当前流中的字符异步读入内存块。Asynchronously reads the characters from the current stream into a memory block.

ReadBlock(Char[], Int32, Int32)

从当前文本读取器中读取指定的最大字符数并从指定索引处开始将该数据写入缓冲区。Reads a specified maximum number of characters from the current text reader and writes the data to a buffer, beginning at the specified index.

ReadBlock(Span<Char>)

从当前流中读取字符并将数据写入缓冲区。Reads the characters from the current stream and writes the data to a buffer.

ReadBlockAsync(Char[], Int32, Int32)

异步从当前文本读取器中读取指定最大字符数并从指定索引开始将该数据写入缓冲区。Reads a specified maximum number of characters from the current text reader asynchronously and writes the data to a buffer, beginning at the specified index.

ReadBlockAsync(Memory<Char>, CancellationToken)

从当前流中异步读取字符并将数据写入缓冲区。Asynchronously reads the characters from the current stream and writes the data to a buffer.

ReadLine()

从文本读取器中读取一行字符并将数据作为字符串返回。Reads a line of characters from the text reader and returns the data as a string.

ReadLineAsync()

异步读取一行字符并将数据作为字符串返回。Reads a line of characters asynchronously and returns the data as a string.

ReadToEnd()

读取从当前位置到文本读取器末尾的所有字符并将它们作为一个字符串返回。Reads all characters from the current position to the end of the text reader and returns them as one string.

ReadToEndAsync()

异步读取从当前位置到文本读取器末尾的所有字符并将它们作为一个字符串返回。Reads all characters from the current position to the end of the text reader asynchronously and returns them as one string.

Synchronized(TextReader)

在指定的 TextReader 周围创建线程安全包装。Creates a thread-safe wrapper around the specified TextReader.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式接口实现

IDisposable.Dispose()

有关此成员的说明,请参阅 Dispose()For a description of this member, see Dispose().

适用于

另请参阅