TextReader TextReader TextReader TextReader Class

定义

表示可读取连续字符系列的读取器。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

注解

TextReader 是的抽象基类StreamReaderStringReader,其中从流和字符串中分别读取字符。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. 若要直接释放类型,调用其Dispose中的方法try / 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). 有关详细信息,请参阅 Dispose 和中的"使用对象实现 IDisposable"一节IDisposable接口主题。For more information, see Dispose and the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

继承者说明

在派生的类必须至少实现Peek()Read()方法,以便进行的有用实例TextReaderA derived class must minimally implement the Peek() and Read() methods to make a useful instance of TextReader.

构造函数

TextReader() TextReader() TextReader() TextReader()

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

字段

Null Null Null Null

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

方法

Close() Close() Close() Close()

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

CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type)

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

(Inherited from MarshalByRefObject)
Dispose() Dispose() Dispose() Dispose()

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

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

释放由 TextReader 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the TextReader and optionally releases the managed resources.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

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

(Inherited from MarshalByRefObject)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

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

(Inherited from MarshalByRefObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

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

(Inherited from MarshalByRefObject)
Peek() Peek() Peek() 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() Read() Read() Read()

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

Read(Char[], Int32, Int32) Read(Char[], Int32, Int32) Read(Char[], Int32, Int32) 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>) Read(Span<Char>) Read(Span<Char>) Read(Span<Char>)
ReadAsync(Char[], Int32, Int32) ReadAsync(Char[], Int32, Int32) ReadAsync(Char[], Int32, Int32) 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) ReadAsync(Memory<Char>, CancellationToken) ReadAsync(Memory<Char>, CancellationToken) ReadAsync(Memory<Char>, CancellationToken)
ReadBlock(Char[], Int32, Int32) ReadBlock(Char[], Int32, Int32) ReadBlock(Char[], Int32, Int32) 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>) ReadBlock(Span<Char>) ReadBlock(Span<Char>) ReadBlock(Span<Char>)
ReadBlockAsync(Char[], Int32, Int32) ReadBlockAsync(Char[], Int32, Int32) ReadBlockAsync(Char[], Int32, Int32) 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) ReadBlockAsync(Memory<Char>, CancellationToken) ReadBlockAsync(Memory<Char>, CancellationToken) ReadBlockAsync(Memory<Char>, CancellationToken)
ReadLine() ReadLine() ReadLine() ReadLine()

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

ReadLineAsync() ReadLineAsync() ReadLineAsync() ReadLineAsync()

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

ReadToEnd() ReadToEnd() ReadToEnd() ReadToEnd()

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

ReadToEndAsync() ReadToEndAsync() ReadToEndAsync() ReadToEndAsync()

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

Synchronized(TextReader) Synchronized(TextReader) Synchronized(TextReader) Synchronized(TextReader)

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

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

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

适用于

另请参阅