TextReader TextReader TextReader TextReader Class

定義

代表可以讀取一連串連續字元的讀取器 (Reader)。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)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。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 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。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()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。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 的淺層複本 (Shallow Copy)。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().

適用於

另請參閱