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 任务。
构造函数
字段
CoreNewLine |
存储用于此 |
Null |
提供 |
属性
AutoFlush |
获取或设置一个值,该值指示 StreamWriter 在每次调用 Write(Char) 之后是否都将其缓冲区刷新到基础流。 |
BaseStream |
获取同后备存储连接的基础流。 |
Encoding |
获取在其中写入输出的 Encoding。 |
FormatProvider |
获取控制格式设置的对象。 (继承自 TextWriter) |
NewLine |
获取或设置由当前 |
方法
显式接口实现
IDisposable.Dispose() |
有关此成员的说明,请参见 Dispose()。 (继承自 TextWriter) |
扩展方法
ConfigureAwait(IAsyncDisposable, Boolean) |
配置如何执行从异步可处置项返回的任务的等待。 |