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 中)等语言构造。 有关详细信息,请参阅接口主题中的 IDisposable Dispose 和“使用实现 IDisposable 的对象”部分。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
实施者说明
派生类必须至少实现 方法, Write(Char) 才能成为 的有用实例 TextWriter。
构造函数
TextWriter() |
初始化 TextWriter 类的新实例。 |
TextWriter(IFormatProvider) |
使用指定的格式提供程序初始化 TextWriter 类的新实例。 |
字段
CoreNewLine |
存储用于此 |
Null |
提供 |
属性
Encoding |
当在派生类中重写时,返回用来写输出的该字符编码。 |
FormatProvider |
获取控制格式设置的对象。 |
NewLine |
获取或设置由当前 |
方法
显式接口实现
IDisposable.Dispose() |
有关此成员的说明,请参见 Dispose()。 |
扩展方法
ConfigureAwait(IAsyncDisposable, Boolean) |
配置如何执行从异步可处置项返回的任务的等待。 |
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈