TextReader 类

定义

表示可读取有序字符系列的读取器。

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

示例

类是一个抽象类。 因此,不要在代码中实例化它。 类 StreamReader 派生自 TextReader ,并提供用于从流读取的成员的实现。 以下示例演示如何使用 StreamReader.ReadAsync(Char[], Int32, Int32) 方法读取文件中的所有字符。 在将字符添加到 类的 StringBuilder 实例之前,它会检查每个字符是字母、数字还是空白。

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是 和 StringReaderStreamReader抽象基类,分别从流和字符串中读取字符。 使用这些派生类打开文本文件以读取指定的字符范围,或基于现有流创建读取器。

重要

此类型实现 IDisposable 接口。 使用完派生自此类型的任何类型后,应直接或间接释放它。 若要直接释放类型,请在 try/catch 块中调用其 Dispose 方法。 若要间接释放类型,请使用 using(在 C# 中)或 Using(在 Visual Basic 中)等语言构造。 有关详细信息,请参阅接口主题中的 Dispose 和“使用实现 IDisposable 的对象”部分 IDisposable

实施者说明

派生类必须至少实现 Peek()Read() 方法,才能成为 有用的 实例 TextReader

构造函数

TextReader()

初始化 TextReader 类的新实例。

字段

Null

提供一个无数据可供读取的 TextReader

方法

Close()

关闭 TextReader 并释放与该 TextReader 关联的所有系统资源。

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。

(继承自 MarshalByRefObject)
Dispose()

释放由 TextReader 对象使用的所有资源。

Dispose(Boolean)

释放由 TextReader 占用的非托管资源,还可以另外再释放托管资源。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetLifetimeService()
已过时.

检索控制此实例的生存期策略的当前生存期服务对象。

(继承自 MarshalByRefObject)
GetType()

获取当前实例的 Type

(继承自 Object)
InitializeLifetimeService()
已过时.

获取生存期服务对象来控制此实例的生存期策略。

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。

(继承自 MarshalByRefObject)
Peek()

读取下一个字符,而不更改读取器状态或字符源。 返回下一个可用字符,而实际上并不从读取器中读取此字符。

Read()

读取文本读取器中的下一个字符并使该字符的位置前移一个字符。

Read(Char[], Int32, Int32)

从当前读取器中读取指定数目的字符并从指定索引开始将该数据写入缓冲区。

Read(Span<Char>)

从当前读取器中读取字符,并将数据写入指定的缓冲区。

ReadAsync(Char[], Int32, Int32)

异步从当前文本读取器中读取指定最大字符数并从指定索引开始将该数据写入缓冲区。

ReadAsync(Memory<Char>, CancellationToken)

将当前流中的字符异步读入内存块。

ReadBlock(Char[], Int32, Int32)

从当前文本读取器中读取指定的最大字符数并从指定索引处开始将该数据写入缓冲区。

ReadBlock(Span<Char>)

从当前流中读取字符并将数据写入缓冲区。

ReadBlockAsync(Char[], Int32, Int32)

异步从当前文本读取器中读取指定最大字符数并从指定索引开始将该数据写入缓冲区。

ReadBlockAsync(Memory<Char>, CancellationToken)

从当前流中异步读取字符并将数据写入缓冲区。

ReadLine()

从文本读取器中读取一行字符并将数据作为字符串返回。

ReadLineAsync()

异步读取一行字符并将数据作为字符串返回。

ReadLineAsync(CancellationToken)

异步读取一行字符并将数据作为字符串返回。

ReadToEnd()

读取从当前位置到文本读取器末尾的所有字符并将它们作为一个字符串返回。

ReadToEndAsync()

异步读取从当前位置到文本读取器末尾的所有字符并将它们作为一个字符串返回。

ReadToEndAsync(CancellationToken)

异步读取从当前位置到文本读取器末尾的所有字符并将它们作为一个字符串返回。

Synchronized(TextReader)

在指定的 TextReader 周围创建线程安全包装。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

IDisposable.Dispose()

有关此成员的说明,请参见 Dispose()

适用于

另请参阅