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. 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.

重写派生类中的 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.

TextWriter 将基元数据类型作为参数的所有 Write 方法将值作为字符串写入。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.SynchronizedSee 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. 若要直接释放类型,请在 Disposetry/ 块中调用其 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). 有关详细信息,请参阅 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)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。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 使用的非托管资源,并选择性地释放托管资源。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()

检索控制此实例的生存期策略的当前生存期服务对象。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 的浅表副本。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()

返回一个表示当前对象的 string。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().

适用于

另请参阅