TextWriter 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表可以寫入一連串連續字元的寫入器。 這個類別是抽象的。
public ref class TextWriter abstract : IDisposable
public ref class TextWriter abstract : MarshalByRefObject, IAsyncDisposable, IDisposable
public ref class TextWriter abstract : MarshalByRefObject, IDisposable
public abstract class TextWriter : IDisposable
public abstract class TextWriter : MarshalByRefObject, IAsyncDisposable, IDisposable
public abstract class TextWriter : MarshalByRefObject, IDisposable
[System.Serializable]
public abstract class TextWriter : MarshalByRefObject, IDisposable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class TextWriter : MarshalByRefObject, IDisposable
type TextWriter = class
interface IDisposable
type TextWriter = class
inherit MarshalByRefObject
interface IAsyncDisposable
interface IDisposable
type TextWriter = class
inherit MarshalByRefObject
interface IDisposable
[<System.Serializable>]
type TextWriter = class
inherit MarshalByRefObject
interface IDisposable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type TextWriter = class
inherit MarshalByRefObject
interface IDisposable
Public MustInherit Class TextWriter
Implements IDisposable
Public MustInherit Class TextWriter
Inherits MarshalByRefObject
Implements IAsyncDisposable, IDisposable
Public MustInherit Class TextWriter
Inherits MarshalByRefObject
Implements IDisposable
- 繼承
-
TextWriter
- 繼承
- 衍生
- 屬性
- 實作
範例
類別是抽象類別, 因此,您的程式碼中不必將它執行個體化。 類別 StreamWriter 衍生自 TextWriter ,並提供寫入資料流程之成員的實作。 下列範例示範如何使用 方法,將包含字串值的兩行寫入文字檔 WriteLineAsync(String) 。
using System.IO;
namespace ConsoleApplication
{
class Program4
{
static void Main()
{
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是 和 StringWriter 的 StreamWriter 抽象基類,分別將字元寫入資料流程和字串。 建立 的實例 TextWriter ,將 物件寫入字串、將字串寫入檔案,或序列化 XML。 您也可以使用 的實例 TextWriter ,使用用於字串或資料流程的相同 API,或新增文字格式設定的支援,將文字寫入自訂支援存放區。
Write
將基本資料類型當做參數的所有方法 TextWriter ,都會寫出值做為字串。
根據預設, TextWriter 不是安全線程。 如需安全線程包裝函式,請參閱 TextWriter.Synchronized 。
重要
此型別代表 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.
如需一般 I/O 工作的清單,請參閱 一般 I/O 工作。
給實施者的注意事項
衍生類別至少必須實作 方法, Write(Char) 才能讓 的實例成為 有用的 TextWriter 實例。
建構函式
TextWriter() |
初始化 TextWriter 類別的新執行個體。 |
TextWriter(IFormatProvider) |
使用指定的格式提供者,初始化 TextWriter 類別的新執行個體。 |
欄位
CoreNewLine |
儲存這個 |
Null |
提供 |
屬性
Encoding |
當在衍生類別中覆寫該屬性時,傳回用於寫入輸出的字元編碼。 |
FormatProvider |
取得控制格式設定的物件。 |
NewLine |
取得或設定目前 |
方法
Close() |
關閉目前寫入器並釋放任何與寫入器相關聯的系統資源。 |
CreateObjRef(Type) |
建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。 (繼承來源 MarshalByRefObject) |
Dispose() |
釋放由 TextWriter 物件使用的所有資源。 |
Dispose(Boolean) |
釋放 TextWriter 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。 |
DisposeAsync() |
以非同步方式釋放由 TextWriter 物件使用的所有資源。 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
Flush() |
清除目前寫入器的所有緩衝區,並造成任何緩衝資料都寫入基礎裝置。 |
FlushAsync() |
以非同步的方式清除目前寫入器的所有緩衝區,並造成任何緩衝資料都寫入基礎裝置。 |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetLifetimeService() |
已過時。
擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。 (繼承來源 MarshalByRefObject) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
InitializeLifetimeService() |
已過時。
取得存留期服務物件,以控制這個執行個體的存留期原則。 (繼承來源 MarshalByRefObject) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
MemberwiseClone(Boolean) |
建立目前 MarshalByRefObject 物件的淺層複本。 (繼承來源 MarshalByRefObject) |
Synchronized(TextWriter) |
在指定的 |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |
Write(Boolean) |
將 |
Write(Char) |
將字元寫入文字資料流。 |
Write(Char[]) |
將字元陣列寫入文字資料流。 |
Write(Char[], Int32, Int32) |
將字元的子陣列寫入至文字資料流。 |
Write(Decimal) |
將十進位值的文字表示寫入文字資料流。 |
Write(Double) |
將 8 位元組浮點數值的文字表示寫入文字資料流。 |
Write(Int32) |
將 4 位元組帶正負號的整數文字表示寫入文字資料流。 |
Write(Int64) |
將 8 位元組帶正負號的整數文字表示寫入文字資料流。 |
Write(Object) |
呼叫該物件的 |
Write(ReadOnlySpan<Char>) |
將字元範圍寫入文字資料流。 |
Write(Single) |
將 4 位元組浮點數值的文字表示寫入文字資料流。 |
Write(String) |
將字串寫入到文字資料流。 |
Write(String, Object) |
使用與 Format(String, Object) 方法相同的語意,將格式化字串寫入文字資料流。 |
Write(String, Object, Object) |
使用與 Format(String, Object, Object) 方法相同的語意,將格式化字串寫入文字資料流。 |
Write(String, Object, Object, Object) |
使用與 Format(String, Object, Object, Object) 方法相同的語意,將格式化字串寫入文字資料流。 |
Write(String, Object[]) |
使用與 Format(String, Object[]) 方法相同的語意,將格式化字串寫入文字資料流。 |
Write(StringBuilder) |
將字串產生器寫入文字資料流。 |
Write(UInt32) |
將 4 位元組不帶正負號的整數文字表示寫入文字資料流。 |
Write(UInt64) |
將 8 位元組帶不正負號的整數文字表示寫入文字資料流。 |
WriteAsync(Char) |
以非同步方式將字元寫入文字資料流。 |
WriteAsync(Char[]) |
以非同步方式將字元陣列寫入文字資料流。 |
WriteAsync(Char[], Int32, Int32) |
以非同步方式將字元的子陣列寫入文字資料流。 |
WriteAsync(ReadOnlyMemory<Char>, CancellationToken) |
以非同步方式將字元記憶體區域寫入文字資料流。 |
WriteAsync(String) |
以非同步方式將字串寫入至文字資料流。 |
WriteAsync(StringBuilder, CancellationToken) |
以非同步方式將字串產生器寫入文字資料流。 |
WriteLine() |
將行結束字元寫入文字資料流。 |
WriteLine(Boolean) |
將 |
WriteLine(Char) |
將字元寫入文字資料流,後接行結束字元。 |
WriteLine(Char[]) |
將字元的陣列寫入文字資料流,後接行結束字元。 |
WriteLine(Char[], Int32, Int32) |
將字元的子陣列寫入文字資料流,後接行結束字元。 |
WriteLine(Decimal) |
將十進位值的文字表示寫入文字資料流,後接行結束字元。 |
WriteLine(Double) |
將 8 位元組浮點數值的文字表示寫入文字資料流,後接行結束字元。 |
WriteLine(Int32) |
將 4 位元組帶正負號的整數文字表示寫入文字資料流,後接行結束字元。 |
WriteLine(Int64) |
將 8 位元組帶正負號的整數文字表示寫入文字資料流,後接行結束字元。 |
WriteLine(Object) |
藉由呼叫該物件上的 |
WriteLine(ReadOnlySpan<Char>) |
將字元範圍的文字表示寫入文字資料流,後接行結束字元。 |
WriteLine(Single) |
將 4 位元組浮點數值的文字表示寫入文字資料流,後接行結束字元。 |
WriteLine(String) |
將字串寫入文字資料流,後接行結束字元。 |
WriteLine(String, Object) |
使用與 Format(String, Object) 方法相同的語意,將格式化字串和新行寫入文字資料流。 |
WriteLine(String, Object, Object) |
使用與 Format(String, Object, Object) 方法相同的語意,將格式化字串和新行寫入文字資料流。 |
WriteLine(String, Object, Object, Object) |
使用與 Format(String, Object) 相同的語意,寫出文字資料流中的格式化字串和新行。 |
WriteLine(String, Object[]) |
使用與 Format(String, Object) 相同的語意,寫出文字資料流中的格式化字串和新行。 |
WriteLine(StringBuilder) |
將字串產生器的文字表示寫入文字資料流,後接行結束字元。 |
WriteLine(UInt32) |
將 4 位元組不帶正負號的整數文字表示寫入文字資料流,後接行結束字元。 |
WriteLine(UInt64) |
將 8 位元組不帶正負號的整數文字表示寫入文字資料流,後接行結束字元。 |
WriteLineAsync() |
以非同步方式將行結束字元寫入文字資料流。 |
WriteLineAsync(Char) |
以非同步方式將字元寫入文字資料流,後接行結束字元。 |
WriteLineAsync(Char[]) |
以非同步方式將字元的陣列寫入文字資料流,後接行結束字元。 |
WriteLineAsync(Char[], Int32, Int32) |
以非同步方式將字元的子陣列寫入文字資料流,後接行結束字元。 |
WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken) |
以非同步方式將字元記憶體區域的文字表示寫入文字資料流,後接行結束字元。 |
WriteLineAsync(String) |
以非同步方式將字串寫入文字資料流,後接行結束字元。 |
WriteLineAsync(StringBuilder, CancellationToken) |
以非同步方式將字串產生器的文字表示寫入文字資料流,後接行結束字元。 |
明確介面實作
IDisposable.Dispose() |
如需這個成員的說明,請參閱 Dispose()。 |
擴充方法
ConfigureAwait(IAsyncDisposable, Boolean) |
設定如何執行從非同步可處置項目傳回的工作 await。 |