StreamWriter 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
实现一个 TextWriter,使其以一种特定的编码向流中写入字符。
public ref class StreamWriter : System::IO::TextWriter
public class StreamWriter : System.IO.TextWriter
[System.Serializable]
public class StreamWriter : System.IO.TextWriter
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class StreamWriter : System.IO.TextWriter
type StreamWriter = class
inherit TextWriter
[<System.Serializable>]
type StreamWriter = class
inherit TextWriter
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StreamWriter = class
inherit TextWriter
Public Class StreamWriter
Inherits TextWriter
- 继承
- 继承
- 属性
示例
以下示例演示如何使用 StreamWriter 对象编写列出 C 驱动器上的目录的文件,然后使用 StreamReader 对象读取和显示每个目录名称。 一个好的做法是在 语句中 using
使用这些对象,以便正确释放非托管资源。 当 using
使用该对象的代码完成时, 语句会自动对对象调用 Dispose 。 不支持在 Windows 应用商店应用中使用此示例中使用的构造函数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace StreamReadWrite
{
class Program
{
static void Main(string[] args)
{
// Get the directories currently on the C drive.
DirectoryInfo[] cDirs = new DirectoryInfo(@"c:\").GetDirectories();
// Write each directory name to a file.
using (StreamWriter sw = new StreamWriter("CDriveDirs.txt"))
{
foreach (DirectoryInfo dir in cDirs)
{
sw.WriteLine(dir.Name);
}
}
// Read and show each line from the file.
string line = "";
using (StreamReader sr = new StreamReader("CDriveDirs.txt"))
{
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}
}
Imports System.IO
Class Program
Shared Sub Main()
' Get the directories currently on the C drive.
Dim cDirs As DirectoryInfo() = New DirectoryInfo("c:\").GetDirectories()
' Write each directory name to a file.
Using sw As StreamWriter = New StreamWriter("CDriveDirs.txt")
For Each Dir As DirectoryInfo In cDirs
sw.WriteLine(Dir.Name)
Next
End Using
'Read and show each line from the file.
Dim line As String = ""
Using sr As StreamReader = New StreamReader("CDriveDirs.txt")
Do
line = sr.ReadLine()
Console.WriteLine(line)
Loop Until line Is Nothing
End Using
End Sub
End Class
注解
StreamWriter 设计用于特定编码中的字符输出,而派生自 Stream 的类则设计用于字节输入和输出。
重要
此类型实现 IDisposable 接口。 在使用完类型后,您应直接或间接释放类型。 若要直接释放类型,请在 try
/catch
块中调用其 Dispose 方法。 若要间接释放类型,请使用 using
(在 C# 中)或 Using
(在 Visual Basic 中)等语言构造。 有关详细信息,请参阅 IDisposable 接口主题中的“使用实现 IDisposable 的对象”一节。
StreamWriter 除非另有指定,否则默认使用 的 UTF8Encoding 实例。 构造的 UTF8Encoding
此实例没有字节顺序标记 (BOM) ,因此其 GetPreamble 方法返回空字节数组。 此构造函数的默认 UTF-8 编码会在无效字节上引发异常。 此行为不同于 属性中的 Encoding.UTF8 编码对象提供的行为。 若要指定 BOM 并确定是否对无效字节引发异常,请使用接受编码对象作为参数的构造函数,例如 StreamWriter(String, Boolean, Encoding) 或 StreamWriter。
默认情况下, StreamWriter 不是线程安全的。 有关线程安全包装器,请参阅 TextWriter.Synchronized 。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
构造函数
StreamWriter(Stream) |
使用 UTF-8 编码及默认的缓冲区大小,为指定的流初始化 StreamWriter 类的新实例。 |
StreamWriter(Stream, Encoding) |
使用指定的编码及默认的缓冲区大小,为指定的流初始化 StreamWriter 类的新实例。 |
StreamWriter(Stream, Encoding, Int32) |
使用指定的编码及缓冲区大小,为指定的流初始化 StreamWriter 类的新实例。 |
StreamWriter(Stream, Encoding, Int32, Boolean) |
使用指定的编码和缓冲区大小,为指定的流初始化 StreamWriter 类的新实例,并可以选择保持流处于打开状态。 |
StreamWriter(String) |
用默认编码和缓冲区大小,为指定的文件初始化 StreamWriter 类的一个新实例。 |
StreamWriter(String, Boolean) |
用默认编码和缓冲区大小,为指定的文件初始化 StreamWriter 类的一个新实例。 如果该文件存在,则可以将其覆盖或向其追加。 如果该文件不存在,此构造函数将创建一个新文件。 |
StreamWriter(String, Boolean, Encoding) |
使用指定的编码和默认的缓冲区大小,为指定的文件初始化 StreamWriter 类的新实例。 如果该文件存在,则可以将其覆盖或向其追加。 如果该文件不存在,此构造函数将创建一个新文件。 |
StreamWriter(String, Boolean, Encoding, Int32) |
使用指定编码和缓冲区大小,为指定路径上的指定文件初始化 StreamWriter 类的新实例。 如果该文件存在,则可以将其覆盖或向其追加。 如果该文件不存在,此构造函数将创建一个新文件。 |
StreamWriter(String, Encoding, FileStreamOptions) |
使用指定的编码为指定文件初始化 类的新实例 StreamWriter ,并使用指定的 FileStreamOptions 对象进行配置。 |
StreamWriter(String, FileStreamOptions) |
使用默认编码为指定文件初始化 类的新实例 StreamWriter ,并使用指定的 FileStreamOptions 对象进行配置。 |
字段
CoreNewLine |
存储用于此 |
Null |
提供 |
属性
AutoFlush |
获取或设置一个值,该值指示 StreamWriter 在每次调用 Write(Char) 之后是否都将其缓冲区刷新到基础流。 |
BaseStream |
获取同后备存储连接的基础流。 |
Encoding |
获取在其中写入输出的 Encoding。 |
FormatProvider |
获取控制格式设置的对象。 (继承自 TextWriter) |
NewLine |
获取或设置由当前 |
方法
显式接口实现
IDisposable.Dispose() |
有关此成员的说明,请参见 Dispose()。 (继承自 TextWriter) |
扩展方法
ConfigureAwait(IAsyncDisposable, Boolean) |
配置如何执行从异步可处置项返回的任务的等待。 |
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈