TextWriter 類別

定義

代表可以寫入一連串連續字元的寫入器。Represents a writer that can write a sequential series of characters. 這個類別是抽象的。This class is abstract.

public ref class TextWriter abstract : MarshalByRefObject, IDisposable
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public abstract class TextWriter : MarshalByRefObject, IDisposable
type TextWriter = class
    inherit MarshalByRefObject
    interface IDisposable
Public MustInherit Class TextWriter
Inherits MarshalByRefObject
Implements IDisposable
繼承
衍生
屬性
實作

範例

TextWriter 類別是抽象類別。The TextWriter class is an abstract class. 因此,您的程式碼中不必將它執行個體化。Therefore, you do not instantiate it in your code. 類別衍生自TextWriter ,並提供成員的執行,以寫入資料流程。 StreamWriterThe StreamWriter class derives from TextWriter and provides implementations of the members for writing to a stream. 下列範例顯示如何使用WriteLineAsync(String)方法,將包含字串值的兩行寫入文字檔。The following example shows how to write two lines that consist of string values to a text file by using the WriteLineAsync(String) method.

覆寫衍生類別中的 Dispose 以提供Override Dispose in a derived class to provide

using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteCharacters();
        }

        static async void WriteCharacters()
        {
            using (StreamWriter writer = File.CreateText("newfile.txt"))
            {
                await writer.WriteLineAsync("First line of example");
                await writer.WriteLineAsync("and second line");
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        WriteCharacters()
    End Sub

    Async Sub WriteCharacters()
        Using writer As StreamWriter = File.CreateText("newfile.txt")
            Await writer.WriteLineAsync("First line of example")
            Await writer.WriteLineAsync("and second line")
        End Using
    End Sub
End Module

備註

TextWriterStreamWriterStringWriter的抽象基類,分別會將字元寫入資料流程和字串。TextWriter is the abstract base class of StreamWriter and StringWriter, which write characters to streams and strings, respectively. 建立的TextWriter實例,以將物件寫入字串、將字串寫入檔案,或序列化 XML。Create an instance of TextWriter to write an object to a string, write strings to a file, or to serialize XML. 您也可以使用的實例TextWriter ,使用您用於字串或資料流程的相同 api,將文字寫入至自訂備份存放區,或加入文字格式的支援。You can also use an instance of TextWriter to write text to a custom backing store using the same APIs you would use for a string or a stream, or to add support for text formatting.

所有具有基本資料類型做為參數的Write TextWriter方法,都會將值寫出為字串。All the Write methods of TextWriter having primitive data types as parameters write out the values as strings.

根據預設, TextWriter不是安全線程。By default, a TextWriter is not thread safe. TextWriter.Synchronized需安全線程的包裝函式,請參閱。See TextWriter.Synchronized for a thread-safe wrapper.

重要

此型別代表 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. 若要直接處置型別,請呼叫其 try/catch 區塊中的 Dispose 方法。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). 如需詳細資訊,請參閱IDisposable介面主題中的 Dispose 和「使用可執行 IDisposable 的物件」一節。For more information, see Dispose and the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

給繼承者的注意事項

衍生的類別必須最低限度地Write(Char)執行方法,才能建立有用的TextWriter實例。A derived class must minimally implement the Write(Char) method to make a useful instance of TextWriter.

建構函式

TextWriter()

初始化 TextWriter 類別的新執行個體。Initializes a new instance of the TextWriter class.

TextWriter(IFormatProvider)

使用指定的格式提供者,初始化 TextWriter 類別的新執行個體。Initializes a new instance of the TextWriter class with the specified format provider.

欄位

CoreNewLine

儲存這個 TextWriter 所使用的新行字元。Stores the newline characters used for this TextWriter.

Null

提供 TextWriter,但不包含可寫入但無法讀取的備份存放區。Provides a TextWriter with no backing store that can be written to, but not read from.

屬性

Encoding

當在衍生類別中覆寫該屬性時,傳回用於寫入輸出的字元編碼。When overridden in a derived class, returns the character encoding in which the output is written.

FormatProvider

取得控制格式設定的物件。Gets an object that controls formatting.

NewLine

取得或設定目前 TextWriter 所使用的行結束字元字串。Gets or sets the line terminator string used by the current TextWriter.

方法

Close()

關閉目前寫入器並釋放任何與寫入器相關聯的系統資源。Closes the current writer and releases any system resources associated with the writer.

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(繼承來源 MarshalByRefObject)
Dispose()

釋放由 TextWriter 物件使用的所有資源。Releases all resources used by the TextWriter object.

Dispose(Boolean)

釋放 TextWriter 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the TextWriter and optionally releases the managed resources.

DisposeAsync()

以非同步方式釋放由 TextWriter 物件使用的所有資源。Asynchronously releases all resources used by the TextWriter object.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
Flush()

清除目前寫入器的所有緩衝區,並造成任何緩衝資料都寫入基礎裝置。Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

FlushAsync()

以非同步的方式清除目前寫入器的所有緩衝區,並造成任何緩衝資料都寫入基礎裝置。Asynchronously clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetLifetimeService()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
InitializeLifetimeService()

取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

(繼承來源 MarshalByRefObject)
Synchronized(TextWriter)

在指定的 TextWriter 內建立安全執行緒的包裝函式。Creates a thread-safe wrapper around the specified TextWriter.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)
Write(Boolean)

Boolean 值的文字表示寫入文字資料流。Writes the text representation of a Boolean value to the text stream.

Write(Char)

將字元寫入文字資料流。Writes a character to the text stream.

Write(Char[])

將字元陣列寫入文字資料流。Writes a character array to the text stream.

Write(Char[], Int32, Int32)

將字元的子陣列寫入至文字資料流。Writes a subarray of characters to the text stream.

Write(Decimal)

將十進位值的文字表示寫入文字資料流。Writes the text representation of a decimal value to the text stream.

Write(Double)

將 8 位元組浮點數值的文字表示寫入文字資料流。Writes the text representation of an 8-byte floating-point value to the text stream.

Write(Int32)

將 4 位元組帶正負號的整數文字表示寫入文字資料流。Writes the text representation of a 4-byte signed integer to the text stream.

Write(Int64)

將 8 位元組帶正負號的整數文字表示寫入文字資料流。Writes the text representation of an 8-byte signed integer to the text stream.

Write(Object)

呼叫該物件的 ToString 方法,將物件的文字表示寫入文字資料流。Writes the text representation of an object to the text stream by calling the ToString method on that object.

Write(ReadOnlySpan<Char>)

將字元範圍寫入文字資料流。Writes a character span to the text stream.

Write(Single)

將 4 位元組浮點數值的文字表示寫入文字資料流。Writes the text representation of a 4-byte floating-point value to the text stream.

Write(String)

將字串寫入到文字資料流。Writes a string to the text stream.

Write(String, Object)

使用與 Format(String, Object) 方法相同的語意,將格式化字串寫入文字資料流。Writes a formatted string to the text stream, using the same semantics as the Format(String, Object) method.

Write(String, Object, Object)

使用與 Format(String, Object, Object) 方法相同的語意,將格式化字串寫入文字資料流。Writes a formatted string to the text stream using the same semantics as the Format(String, Object, Object) method.

Write(String, Object, Object, Object)

使用與 Format(String, Object, Object, Object) 方法相同的語意,將格式化字串寫入文字資料流。Writes a formatted string to the text stream, using the same semantics as the Format(String, Object, Object, Object) method.

Write(String, Object[])

使用與 Format(String, Object[]) 方法相同的語意,將格式化字串寫入文字資料流。Writes a formatted string to the text stream, using the same semantics as the Format(String, Object[]) method.

Write(StringBuilder)

將字串產生器寫入文字資料流。Writes a string builder to the text stream.

Write(UInt32)

將 4 位元組不帶正負號的整數文字表示寫入文字資料流。Writes the text representation of a 4-byte unsigned integer to the text stream.

Write(UInt64)

將 8 位元組帶不正負號的整數文字表示寫入文字資料流。Writes the text representation of an 8-byte unsigned integer to the text stream.

WriteAsync(Char)

以非同步方式將字元寫入文字資料流。Writes a character to the text stream asynchronously.

WriteAsync(Char[])

以非同步方式將字元陣列寫入文字資料流。Writes a character array to the text stream asynchronously.

WriteAsync(Char[], Int32, Int32)

以非同步方式將字元的子陣列寫入文字資料流。Writes a subarray of characters to the text stream asynchronously.

WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

以非同步方式將字元記憶體區域寫入文字資料流。Asynchronously writes a character memory region to the text stream.

WriteAsync(String)

以非同步方式將字串寫入至文字資料流。Writes a string to the text stream asynchronously.

WriteAsync(StringBuilder, CancellationToken)

以非同步方式將字串產生器寫入文字資料流。Asynchronously writes a string builder to the text stream.

WriteLine()

將行結束字元寫入文字資料流。Writes a line terminator to the text stream.

WriteLine(Boolean)

Boolean 值的文字表示寫入文字資料流,後接行結束字元。Writes the text representation of a Boolean value to the text stream, followed by a line terminator.

WriteLine(Char)

將字元寫入文字資料流,後接行結束字元。Writes a character to the text stream, followed by a line terminator.

WriteLine(Char[])

將字元的陣列寫入文字資料流,後接行結束字元。Writes an array of characters to the text stream, followed by a line terminator.

WriteLine(Char[], Int32, Int32)

將字元的子陣列寫入文字資料流,後接行結束字元。Writes a subarray of characters to the text stream, followed by a line terminator.

WriteLine(Decimal)

將十進位值的文字表示寫入文字資料流,後接行結束字元。Writes the text representation of a decimal value to the text stream, followed by a line terminator.

WriteLine(Double)

將 8 位元組浮點數值的文字表示寫入文字資料流,後接行結束字元。Writes the text representation of a 8-byte floating-point value to the text stream, followed by a line terminator.

WriteLine(Int32)

將 4 位元組帶正負號的整數文字表示寫入文字資料流,後接行結束字元。Writes the text representation of a 4-byte signed integer to the text stream, followed by a line terminator.

WriteLine(Int64)

將 8 位元組帶正負號的整數文字表示寫入文字資料流,後接行結束字元。Writes the text representation of an 8-byte signed integer to the text stream, followed by a line terminator.

WriteLine(Object)

藉由呼叫該物件上的 ToString 方法,後接行結束字元,將物件的文字表示寫入文字資料流。Writes the text representation of an object to the text stream, by calling the ToString method on that object, followed by a line terminator.

WriteLine(ReadOnlySpan<Char>)

將字元範圍的文字表示寫入文字資料流,後接行結束字元。Writes the text representation of a character span to the text stream, followed by a line terminator.

WriteLine(Single)

將 4 位元組浮點數值的文字表示寫入文字資料流,後接行結束字元。Writes the text representation of a 4-byte floating-point value to the text stream, followed by a line terminator.

WriteLine(String)

將字串寫入文字資料流,後接行結束字元。Writes a string to the text stream, followed by a line terminator.

WriteLine(String, Object)

使用與 Format(String, Object) 方法相同的語意,將格式化字串和新行寫入文字資料流。Writes a formatted string and a new line to the text stream, using the same semantics as the Format(String, Object) method.

WriteLine(String, Object, Object)

使用與 Format(String, Object, Object) 方法相同的語意,將格式化字串和新行寫入文字資料流。Writes a formatted string and a new line to the text stream, using the same semantics as the Format(String, Object, Object) method.

WriteLine(String, Object, Object, Object)

使用與 Format(String, Object) 相同的語意,寫出文字資料流中的格式化字串和新行。Writes out a formatted string and a new line to the text stream, using the same semantics as Format(String, Object).

WriteLine(String, Object[])

使用與 Format(String, Object) 相同的語意,寫出文字資料流中的格式化字串和新行。Writes out a formatted string and a new line to the text stream, using the same semantics as Format(String, Object).

WriteLine(StringBuilder)

將字串產生器的文字表示寫入文字資料流,後接行結束字元。Writes the text representation of a string builder to the text stream, followed by a line terminator.

WriteLine(UInt32)

將 4 位元組不帶正負號的整數文字表示寫入文字資料流,後接行結束字元。Writes the text representation of a 4-byte unsigned integer to the text stream, followed by a line terminator.

WriteLine(UInt64)

將 8 位元組不帶正負號的整數文字表示寫入文字資料流,後接行結束字元。Writes the text representation of an 8-byte unsigned integer to the text stream, followed by a line terminator.

WriteLineAsync()

以非同步方式將行結束字元寫入文字資料流。Asynchronously writes a line terminator to the text stream.

WriteLineAsync(Char)

以非同步方式將字元寫入文字資料流,後接行結束字元。Asynchronously writes a character to the text stream, followed by a line terminator.

WriteLineAsync(Char[])

以非同步方式將字元的陣列寫入文字資料流,後接行結束字元。Asynchronously writes an array of characters to the text stream, followed by a line terminator.

WriteLineAsync(Char[], Int32, Int32)

以非同步方式將字元的子陣列寫入文字資料流,後接行結束字元。Asynchronously writes a subarray of characters to the text stream, followed by a line terminator.

WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

以非同步方式將字元記憶體區域的文字表示寫入文字資料流,後接行結束字元。Asynchronously writes the text representation of a character memory region to the text stream, followed by a line terminator.

WriteLineAsync(String)

以非同步方式將字串寫入文字資料流,後接行結束字元。Asynchronously writes a string to the text stream, followed by a line terminator.

WriteLineAsync(StringBuilder, CancellationToken)

以非同步方式將字串產生器的文字表示寫入文字資料流,後接行結束字元。Asynchronously writes the text representation of a string builder to the text stream, followed by a line terminator.

明確介面實作

IDisposable.Dispose()

如需這個成員的說明,請參閱 Dispose()For a description of this member, see Dispose().

適用於

另請參閱