TextReader 類別

定義

代表可以讀取一連串連續字元的讀取器 (Reader)。

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 中)。 For more information, see Dispose and the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

給實施者的注意事項

衍生類別必須以最少方式實 Peek() 作 和 Read() 方法,以建立 有用的 TextReader 實例。

建構函式

TextReader()

初始化 TextReader 類別的新執行個體。

欄位

Null

提供沒有可從中讀取之資料的 TextReader

方法

Close()

關閉 TextReader 並釋放任何與 TextReader 關聯的系統資源。

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Dispose()

釋放由 TextReader 物件使用的所有資源。

Dispose(Boolean)

釋放 TextReader 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 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()

適用於

另請參閱