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. 类派生自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() 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)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。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 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the TextWriter and optionally releases the managed resources.

DisposeAsync() DisposeAsync() DisposeAsync() DisposeAsync()

异步释放由 TextWriter 对象使用的所有资源。Asynchronously releases all resources used by the TextWriter object.

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

检索控制此实例的生存期策略的当前生存期服务对象。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 stream.

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

将字符写入文本流。Writes a character to the text stream.

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

将字符数组写入文本流。Writes a character array to the text 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 stream.

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

将小数值的文本表示形式写入文本流。Writes the text representation of a decimal value to the text stream.

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

将 8 字节浮点值的文本表示形式写入文本流。Writes the text representation of an 8-byte floating-point value to the text stream.

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

将 4 字节带符号整数的文本表示形式写入文本流。Writes the text representation of a 4-byte signed integer to the text stream.

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

将 8 字节带符号整数的文本表示形式写入文本流。Writes the text representation of an 8-byte signed integer to the text stream.

Write(Object) Write(Object) Write(Object) 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>) Write(ReadOnlySpan<Char>) Write(ReadOnlySpan<Char>) Write(ReadOnlySpan<Char>)

将字符范围写入文本流。Writes a character span to the text stream.

Write(Single) Write(Single) Write(Single) Write(Single)

将 4 字节浮点值的文本表示形式写入文本流。Writes the text representation of a 4-byte floating-point value to the text stream.

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

将字符串写入文本流。Writes a string to the text stream.

Write(String, Object) Write(String, Object) Write(String, Object) 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) Write(String, Object, Object) Write(String, Object, Object) 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) 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 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 stream, using the same semantics as the Format(String, Object[]) method.

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

将字符串生成器写入文本流。Writes a string builder to the text stream.

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

将 4 字节无符号整数的文本表示形式写入文本流。Writes the text representation of a 4-byte unsigned integer to the text stream.

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

将 8 字节无符号整数的文本表示形式写入文本流。Writes the text representation of an 8-byte unsigned integer to the text stream.

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

将字符异步写入文本流。Writes a character to the text stream asynchronously.

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

将字符数组异步写入文本流。Writes a character array to the text 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 stream asynchronously.

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

以异步形式将字符内存区域写入文本流。Asynchronously writes a character memory region to the text stream.

WriteAsync(String) WriteAsync(String) WriteAsync(String) WriteAsync(String)

将字符串异步写入文本流。Writes a string to the text stream asynchronously.

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

以异步形式将字符串生成器写入文本流。Asynchronously writes a string builder to the text stream.

WriteLine() WriteLine() WriteLine() WriteLine()

将行终止符写入文本流。Writes a line terminator to the text stream.

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

Boolean 值的文本表示形式写入文本流,后跟行终止符。Writes the text representation of a Boolean value to the text stream, followed by a line terminator.

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

将字符写入文本流,后跟行终止符。Writes a character to the text stream, followed by a line terminator.

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

将字符数组写入文本流,后跟行终止符。Writes an array of characters to the text stream, followed by a line terminator.

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

将字符子数组写入文本流,后跟行终止符。Writes a subarray of characters to the text stream, followed by a line terminator.

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

将小数值的文本表示形式写入文本流,后跟行终止符。Writes the text representation of a decimal value to the text stream, followed by a line terminator.

WriteLine(Double) WriteLine(Double) WriteLine(Double) 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) WriteLine(Int32) WriteLine(Int32) WriteLine(Int32)

将 4 字节带符号整数的文本表示形式写入文本流,后跟行终止符。Writes the text representation of a 4-byte signed integer to the text stream, followed by a line terminator.

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

将 8 字节带符号整数的文本表示形式写入文本流,后跟行终止符。Writes the text representation of an 8-byte signed integer to the text stream, followed by a line terminator.

WriteLine(Object) WriteLine(Object) WriteLine(Object) 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>) WriteLine(ReadOnlySpan<Char>) WriteLine(ReadOnlySpan<Char>) WriteLine(ReadOnlySpan<Char>)

将字符范围的文本表示形式写入文本流,后跟行终止符。Writes the text representation of a character span to the text stream, followed by a line terminator.

WriteLine(Single) WriteLine(Single) WriteLine(Single) 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) WriteLine(String) WriteLine(String) WriteLine(String)

将字符串写入文本流,后跟行终止符。Writes a string to the text stream, followed by a line terminator.

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 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 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 to the text stream, 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 to the text stream, using the same semantics as Format(String, Object).

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

将字符串生成器的文本表示形式写入文本流,后跟行终止符。Writes the text representation of a string builder to the text stream, followed by a line terminator.

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

将 4 字节无符号整数的文本表示形式写入文本流,后跟行终止符。Writes the text representation of a 4-byte unsigned integer to the text stream, followed by a line terminator.

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

将 8 字节无符号整数的文本表示形式写入文本流,后跟行终止符。Writes the text representation of an 8-byte unsigned integer to the text stream, followed by a line terminator.

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

以异步形式将行终止符写入文本流。Asynchronously writes a line terminator to the text stream.

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

以异步形式将字符写入文本流,后跟行终止符。Asynchronously writes a character to the text stream, followed by a line terminator.

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

以异步形式将字符数组写入文本流,后跟行终止符。Asynchronously writes an array of characters to the text stream, followed by a line terminator.

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

以异步形式将字符子数组写入文本流,后跟行终止符。Asynchronously writes a subarray of characters to the text stream, followed by a line terminator.

WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken) WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken) WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken) 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) WriteLineAsync(String) WriteLineAsync(String) WriteLineAsync(String)

以异步形式将字符串写入文本流,后跟行终止符。Asynchronously writes a string to the text stream, followed by a line terminator.

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

以异步形式将字符串生成器的文本表示形式写入文本流,后跟行终止符。Asynchronously writes the text representation of a string builder to the text stream, followed by a line terminator.

显式界面实现

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

有关此成员的说明,请参见 Dispose()For a description of this member, see Dispose().

适用于

另请参阅