TextWriter TextWriter TextWriter TextWriter Class

定義

代表可以寫入一連串連續字元的寫入器。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. StreamWriter類別衍生自TextWriter,並提供成員的實作,以寫入資料流。The 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.

提供衍生類別中覆寫 DisposeOverride 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

備註

TextWriter 是抽象基底類別StreamWriterStringWriter,這將字元寫入到資料流和字串,分別。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. 若要直接處置型別,呼叫其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.

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

給繼承者的注意事項

在衍生的類別都至少必須實作Write(Char)方法以進行實用的執行個體的TextWriterA derived class must minimally implement the Write(Char) method to make a useful instance of TextWriter.

建構函式

TextWriter() TextWriter() TextWriter() TextWriter()

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

TextWriter(IFormatProvider) TextWriter(IFormatProvider) TextWriter(IFormatProvider) TextWriter(IFormatProvider)

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

欄位

CoreNewLine CoreNewLine CoreNewLine CoreNewLine

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

Null Null Null Null

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

屬性

Encoding Encoding Encoding Encoding

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

FormatProvider FormatProvider FormatProvider FormatProvider

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

NewLine NewLine NewLine NewLine

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

方法

Close() Close() Close() Close()

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

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

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

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

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

DisposeAsync() DisposeAsync() DisposeAsync() DisposeAsync()
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
Flush() Flush() Flush() Flush()

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

FlushAsync() FlushAsync() FlushAsync() FlushAsync()

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

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 的淺層複製。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)
Synchronized(TextWriter) Synchronized(TextWriter) Synchronized(TextWriter) Synchronized(TextWriter)

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

ToString() ToString() ToString() ToString()

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

(Inherited from Object)
Write(Boolean) Write(Boolean) Write(Boolean) Write(Boolean)

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

Write(Char) Write(Char) Write(Char) Write(Char)

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

Write(Char[]) Write(Char[]) Write(Char[]) Write(Char[])

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

Write(Char[], Int32, Int32) Write(Char[], Int32, Int32) Write(Char[], Int32, Int32) Write(Char[], Int32, Int32)

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

Write(Decimal) Write(Decimal) Write(Decimal) Write(Decimal)

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

Write(Double) Write(Double) Write(Double) Write(Double)

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

Write(Int32) Write(Int32) Write(Int32) Write(Int32)

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

Write(Int64) Write(Int64) Write(Int64) Write(Int64)

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

Write(Object) Write(Object) Write(Object) Write(Object)

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

Write(ReadOnlySpan<Char>) Write(ReadOnlySpan<Char>) Write(ReadOnlySpan<Char>) Write(ReadOnlySpan<Char>)
Write(Single) Write(Single) Write(Single) Write(Single)

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

Write(String) Write(String) Write(String) Write(String)

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

Write(String, Object) Write(String, Object) Write(String, Object) Write(String, Object)

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

Write(String, Object, Object) Write(String, Object, Object) Write(String, Object, Object) Write(String, Object, Object)

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

Write(String, Object, Object, Object) Write(String, Object, Object, Object) Write(String, Object, Object, Object) Write(String, Object, Object, Object)

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

Write(String, Object[]) Write(String, Object[]) Write(String, Object[]) Write(String, Object[])

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

Write(StringBuilder) Write(StringBuilder) Write(StringBuilder) Write(StringBuilder)
Write(UInt32) Write(UInt32) Write(UInt32) Write(UInt32)

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

Write(UInt64) Write(UInt64) Write(UInt64) Write(UInt64)

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

WriteAsync(Char) WriteAsync(Char) WriteAsync(Char) WriteAsync(Char)

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

WriteAsync(Char[]) WriteAsync(Char[]) WriteAsync(Char[]) WriteAsync(Char[])

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

WriteAsync(Char[], Int32, Int32) WriteAsync(Char[], Int32, Int32) WriteAsync(Char[], Int32, Int32) WriteAsync(Char[], Int32, Int32)

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

WriteAsync(ReadOnlyMemory<Char>, CancellationToken) WriteAsync(ReadOnlyMemory<Char>, CancellationToken) WriteAsync(ReadOnlyMemory<Char>, CancellationToken) WriteAsync(ReadOnlyMemory<Char>, CancellationToken)
WriteAsync(String) WriteAsync(String) WriteAsync(String) WriteAsync(String)

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

WriteAsync(StringBuilder, CancellationToken) WriteAsync(StringBuilder, CancellationToken) WriteAsync(StringBuilder, CancellationToken) WriteAsync(StringBuilder, CancellationToken)
WriteLine() WriteLine() WriteLine() WriteLine()

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

WriteLine(Boolean) WriteLine(Boolean) WriteLine(Boolean) WriteLine(Boolean)

Boolean 值的文字表示,並加上行結束字元寫入到文字字串或資料流。Writes the text representation of a Boolean value followed by a line terminator to the text string or stream.

WriteLine(Char) WriteLine(Char) WriteLine(Char) WriteLine(Char)

將一個字元 (其後加上行結束字元) 寫入到文字字串或資料流。Writes a character followed by a line terminator to the text string or stream.

WriteLine(Char[]) WriteLine(Char[]) WriteLine(Char[]) WriteLine(Char[])

將一個字元 (其後加上行結束字元) 寫入到文字字串或資料流。Writes an array of characters followed by a line terminator to the text string or stream.

WriteLine(Char[], Int32, Int32) WriteLine(Char[], Int32, Int32) WriteLine(Char[], Int32, Int32) WriteLine(Char[], Int32, Int32)

將字元子陣列 (其後加上行結束字元) 寫入到文字字串或資料流。Writes a subarray of characters followed by a line terminator to the text string or stream.

WriteLine(Decimal) WriteLine(Decimal) WriteLine(Decimal) WriteLine(Decimal)

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

WriteLine(Double) WriteLine(Double) WriteLine(Double) WriteLine(Double)

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

WriteLine(Int32) WriteLine(Int32) WriteLine(Int32) WriteLine(Int32)

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

WriteLine(Int64) WriteLine(Int64) WriteLine(Int64) WriteLine(Int64)

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

WriteLine(Object) WriteLine(Object) WriteLine(Object) WriteLine(Object)

透過呼叫該物件上的 ToString 方法,將該物件的文字表示加上行結束字元,寫入到文字字串或資料流。Writes the text representation of an object by calling the ToString method on that object, followed by a line terminator to the text string or stream.

WriteLine(ReadOnlySpan<Char>) WriteLine(ReadOnlySpan<Char>) WriteLine(ReadOnlySpan<Char>) WriteLine(ReadOnlySpan<Char>)
WriteLine(Single) WriteLine(Single) WriteLine(Single) WriteLine(Single)

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

WriteLine(String) WriteLine(String) WriteLine(String) WriteLine(String)

將字串 (其後加上行結束字元) 寫入到文字字串或資料流。Writes a string followed by a line terminator to the text string or stream.

WriteLine(String, Object) WriteLine(String, Object) WriteLine(String, Object) WriteLine(String, Object)

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

WriteLine(String, Object, Object) WriteLine(String, Object, Object) WriteLine(String, Object, Object) WriteLine(String, Object, Object)

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

WriteLine(String, Object, Object, Object) WriteLine(String, Object, Object, Object) WriteLine(String, Object, Object, Object) WriteLine(String, Object, Object, Object)

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

WriteLine(String, Object[]) WriteLine(String, Object[]) WriteLine(String, Object[]) WriteLine(String, Object[])

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

WriteLine(StringBuilder) WriteLine(StringBuilder) WriteLine(StringBuilder) WriteLine(StringBuilder)
WriteLine(UInt32) WriteLine(UInt32) WriteLine(UInt32) WriteLine(UInt32)

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

WriteLine(UInt64) WriteLine(UInt64) WriteLine(UInt64) WriteLine(UInt64)

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

WriteLineAsync() WriteLineAsync() WriteLineAsync() WriteLineAsync()

以非同步方式將行結束字元寫入到文字字串或資料流。Writes a line terminator asynchronously to the text string or stream.

WriteLineAsync(Char) WriteLineAsync(Char) WriteLineAsync(Char) WriteLineAsync(Char)

以非同步方式將一個字元 (其後加上行結束字元) 寫入到文字字串或資料流。Writes a character followed by a line terminator asynchronously to the text string or stream.

WriteLineAsync(Char[]) WriteLineAsync(Char[]) WriteLineAsync(Char[]) WriteLineAsync(Char[])

以非同步方式將字元子陣列 (其後加上行結束字元) 寫入到文字字串或資料流。Writes an array of characters followed by a line terminator asynchronously to the text string or stream.

WriteLineAsync(Char[], Int32, Int32) WriteLineAsync(Char[], Int32, Int32) WriteLineAsync(Char[], Int32, Int32) WriteLineAsync(Char[], Int32, Int32)

以非同步方式將字元子陣列 (其後加上行結束字元) 寫入到文字字串或資料流。Writes a subarray of characters followed by a line terminator asynchronously to the text string or stream.

WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken) WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken) WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken) WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)
WriteLineAsync(String) WriteLineAsync(String) WriteLineAsync(String) WriteLineAsync(String)

以非同步方式將字串 (其後加上行結束字元) 寫入到文字字串或資料流。Writes a string followed by a line terminator asynchronously to the text string or stream.

WriteLineAsync(StringBuilder, CancellationToken) WriteLineAsync(StringBuilder, CancellationToken) WriteLineAsync(StringBuilder, CancellationToken) WriteLineAsync(StringBuilder, CancellationToken)

明確介面實作

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

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

適用於

另請參閱