TextReader 类

定义

表示可以读取顺序序列字符的读取器。

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

示例

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

Important

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

实施者说明

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

构造函数

名称 说明
TextReader()

初始化 TextReader 类的新实例。

字段

名称 说明
Null

提供一个 TextReader 没有要从中读取的数据。

方法

名称 说明
Close()

关闭并释放与 <a0/a0> 关联的任何系统资源。

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

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

ReadToEnd()

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

ReadToEndAsync()

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

Synchronized(TextReader)

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

ToString()

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

(继承自 Object)

显式接口实现

名称 说明
IDisposable.Dispose()

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

适用于

另请参阅