StreamWriter 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 StreamWriter 类的新实例。
重载
StreamWriter(Stream)
使用 UTF-8 编码及默认的缓冲区大小,为指定的流初始化 StreamWriter 类的新实例。
public:
StreamWriter(System::IO::Stream ^ stream);
public StreamWriter (System.IO.Stream stream);
new System.IO.StreamWriter : System.IO.Stream -> System.IO.StreamWriter
Public Sub New (stream As Stream)
参数
- stream
- Stream
要写入的流。
例外
stream
不可写。
stream
为 null
。
示例
下面的代码示例演示了此构造函数。
using System;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string fileName = "test.txt";
string textToAdd = "Example text in file";
FileStream fs = null;
try
{
fs = new FileStream(fileName, FileMode.CreateNew);
using (StreamWriter writer = new StreamWriter(fs))
{
writer.Write(textToAdd);
}
}
finally
{
if (fs != null)
fs.Dispose();
}
}
}
}
Imports System.IO
Module Module1
Sub Main()
Dim fileName As String = "test.txt"
Dim textToAdd As String = "Example text in file"
Dim fs As FileStream = Nothing
Try
fs = New FileStream(fileName, FileMode.CreateNew)
Using writer As StreamWriter = New StreamWriter(fs)
writer.Write(textToAdd)
End Using
Finally
If Not fs Is Nothing Then
fs.Dispose()
End If
End Try
End Sub
End Module
注解
此构造函数 StreamWriter 使用 utf-8 编码创建一个 Byte-Order 标记 (BOM) ,因此其 GetPreamble 方法返回一个空字节数组。 此构造函数的默认 UTF-8 编码对无效字节引发异常。 此行为不同于属性中的编码对象提供的行为 Encoding.UTF8 。 若要指定是否在无效字节上引发异常,请使用接受编码对象作为参数的构造函数,例如 StreamWriter 。 BaseStream属性使用参数进行初始化 stream
。 不重置流的位置。
StreamWriter调用时,对象对 Dispose() 提供的 Stream 对象调用 StreamWriter.Dispose 。
注意
使用特定的区域性设置编译一组字符并使用不同的区域性设置检索这些相同的字符时,这些字符可能无法可解释,并且可能会导致引发异常。
有关常见 i/o 任务的列表,请参阅 常见 I/o 任务。
另请参阅
适用于
StreamWriter(String)
用默认编码和缓冲区大小,为指定的文件初始化 StreamWriter 类的一个新实例。
public:
StreamWriter(System::String ^ path);
public StreamWriter (string path);
new System.IO.StreamWriter : string -> System.IO.StreamWriter
Public Sub New (path As String)
参数
- path
- String
要写入的完整文件路径。 path
可以是一个文件名。
例外
访问被拒绝。
path
为 null
。
指定的路径无效(例如,它位于未映射的驱动器上)。
指定的路径和/或文件名超过了系统定义的最大长度。
path
包含不正确或无效的文件名、目录名或卷标签的语法。
调用方没有所要求的权限。
示例
下面的代码示例演示了此构造函数。
using System;
using System.IO;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string fileName = "test.txt";
string textToAdd = "Example text in file";
using (StreamWriter writer = new StreamWriter(fileName))
{
writer.Write(textToAdd);
}
}
}
}
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim fileName As String = "test.txt"
Dim textToAdd As String = "Example text in file"
Using writer As StreamWriter = New StreamWriter(fileName)
writer.Write(textToAdd)
End Using
End Sub
End Module
注解
此构造函数 StreamWriter 使用 utf-8 编码创建一个 Byte-Order 标记 (BOM) ,因此其 GetPreamble 方法返回一个空字节数组。 此构造函数的默认 UTF-8 编码对无效字节引发异常。 此行为不同于属性中的编码对象提供的行为 Encoding.UTF8 。 若要指定一个 BOM 并确定无效字节是否引发了异常,请使用接受编码对象作为参数的构造函数,例如 StreamWriter(String, Boolean, Encoding) 。
path
参数可以是文件名,包括通用命名约定中的文件 (UNC) 共享。 如果该文件存在,则会被覆盖;否则,将创建新的文件。
path
参数不需要是存储在磁盘上的文件; 它可以是系统的任何支持使用流进行访问的部分。
注意
使用特定的区域性设置编译一组字符并使用不同的区域性设置检索这些相同的字符时,这些字符可能无法可解释,并且可能会导致引发异常。
有关常见 i/o 任务的列表,请参阅 常见 I/o 任务。
另请参阅
适用于
StreamWriter(Stream, Encoding)
使用指定的编码及默认的缓冲区大小,为指定的流初始化 StreamWriter 类的新实例。
public:
StreamWriter(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding);
public StreamWriter (System.IO.Stream stream, System.Text.Encoding encoding);
new System.IO.StreamWriter : System.IO.Stream * System.Text.Encoding -> System.IO.StreamWriter
Public Sub New (stream As Stream, encoding As Encoding)
参数
- stream
- Stream
要写入的流。
- encoding
- Encoding
要使用的字符编码。
例外
stream
或 encoding
为 null
。
stream
不可写。
示例
下面的示例演示了此构造函数。
using System;
using System.IO;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string fileName = "test.txt";
string textToAdd = "Example text in file";
FileStream fs = null;
try
{
fs = new FileStream(fileName, FileMode.CreateNew);
using (StreamWriter writer = new StreamWriter(fs, Encoding.Default))
{
writer.Write(textToAdd);
}
}
finally
{
if (fs != null)
fs.Dispose();
}
}
}
}
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim fileName As String = "test.txt"
Dim textToAdd As String = "Example text in file"
Dim fs As FileStream = Nothing
Try
fs = New FileStream(fileName, FileMode.CreateNew)
Using writer As StreamWriter = New StreamWriter(fs, Encoding.Default)
writer.Write(textToAdd)
End Using
Finally
If Not fs Is Nothing Then
fs.Dispose()
End If
End Try
End Sub
End Module
注解
此构造函数 Encoding 使用 encoding 参数和 BaseStream 使用 stream 参数的属性初始化属性。 不重置流的位置。 有关附加信息,请参见 Encoding。
StreamWriter调用时,对象对 Dispose() 提供的 Stream 对象调用 StreamWriter.Dispose 。
注意
使用特定的区域性设置编译一组字符并使用不同的区域性设置检索这些相同的字符时,这些字符可能不是可解释,并且可能会导致引发异常。
有关常见 i/o 任务的列表,请参阅 常见 I/o 任务。
另请参阅
适用于
StreamWriter(String, Boolean)
用默认编码和缓冲区大小,为指定的文件初始化 StreamWriter 类的一个新实例。 如果该文件存在,则可以将其覆盖或向其追加。 如果该文件不存在,此构造函数将创建一个新文件。
public:
StreamWriter(System::String ^ path, bool append);
public StreamWriter (string path, bool append);
new System.IO.StreamWriter : string * bool -> System.IO.StreamWriter
Public Sub New (path As String, append As Boolean)
参数
- path
- String
要写入的完整文件路径。
- append
- Boolean
若要追加数据到该文件中,则为 true
;若要覆盖该文件,则为 false
。 如果指定的文件不存在,该参数无效,且构造函数将创建一个新文件。
例外
访问被拒绝。
path
为 null
。
指定的路径无效(例如,它位于未映射的驱动器上)。
path
包含不正确或无效的文件名、目录名或卷标签的语法。
指定的路径和/或文件名超过了系统定义的最大长度。
调用方没有所要求的权限。
示例
下面的代码示例演示了此构造函数。
using System;
using System.IO;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string fileName = "test.txt";
string textToAdd = "Example text in file";
using (StreamWriter writer = new StreamWriter(fileName, true))
{
writer.Write(textToAdd);
}
}
}
}
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim fileName As String = "test.txt"
Dim textToAdd As String = "Example text in file"
Using writer As StreamWriter = New StreamWriter(fileName, True)
writer.Write(textToAdd)
End Using
End Sub
End Module
注解
此构造函数 StreamWriter 使用 utf-8 编码创建一个 Byte-Order 标记 (BOM) ,因此其 GetPreamble 方法返回一个空字节数组。 此构造函数的默认 UTF-8 编码对无效字节引发异常。 此行为不同于属性中的编码对象提供的行为 Encoding.UTF8 。 若要指定一个 BOM 并确定无效字节是否引发了异常,请使用接受编码对象作为参数的构造函数,例如 StreamWriter(String, Boolean, Encoding) 。
path
参数可以是文件名,包括通用命名约定中的文件 (UNC) 共享。
path
参数不需要是存储在磁盘上的文件; 它可以是系统的任何支持使用流进行访问的部分。
注意
使用特定的区域性设置编译一组字符并使用不同的区域性设置检索这些相同的字符时,这些字符可能不是可解释,并且可能会导致引发异常。
有关常见 i/o 任务的列表,请参阅 常见 I/o 任务。
另请参阅
适用于
StreamWriter(String, FileStreamOptions)
使用默认编码并配置指定的 对象,为指定文件初始化 类 StreamWriter 的新 FileStreamOptions 实例。
public:
StreamWriter(System::String ^ path, System::IO::FileStreamOptions ^ options);
public StreamWriter (string path, System.IO.FileStreamOptions options);
new System.IO.StreamWriter : string * System.IO.FileStreamOptions -> System.IO.StreamWriter
Public Sub New (path As String, options As FileStreamOptions)
参数
- path
- String
要写入的完整文件路径。
- options
- FileStreamOptions
一个 对象,该对象指定基础 的配置选项 FileStream 。
例外
options
为 null
。
stream
不可写。
另请参阅
适用于
StreamWriter(Stream, Encoding, Int32)
使用指定的编码及缓冲区大小,为指定的流初始化 StreamWriter 类的新实例。
public:
StreamWriter(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, int bufferSize);
public StreamWriter (System.IO.Stream stream, System.Text.Encoding encoding, int bufferSize);
new System.IO.StreamWriter : System.IO.Stream * System.Text.Encoding * int -> System.IO.StreamWriter
Public Sub New (stream As Stream, encoding As Encoding, bufferSize As Integer)
参数
- stream
- Stream
要写入的流。
- encoding
- Encoding
要使用的字符编码。
- bufferSize
- Int32
缓冲区大小(以字节为单位)。
例外
stream
或 encoding
为 null
。
bufferSize
为负数。
stream
不可写。
示例
下面的示例演示此构造函数。
using System;
using System.IO;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string fileName = "test.txt";
string textToAdd = "Example text in file";
FileStream fs = null;
try
{
fs = new FileStream(fileName, FileMode.CreateNew);
using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8, 512))
{
writer.Write(textToAdd);
}
}
finally
{
if (fs != null)
fs.Dispose();
}
}
}
}
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim fileName As String = "test.txt"
Dim textToAdd As String = "Example text in file"
Dim fs As FileStream = Nothing
Try
fs = New FileStream(fileName, FileMode.CreateNew)
Using writer As StreamWriter = New StreamWriter(fs, Encoding.Default, 512)
writer.Write(textToAdd)
End Using
Finally
If Not fs Is Nothing Then
fs.Dispose()
End If
End Try
End Sub
End Module
注解
此构造函数使用 Encoding 参数初始化 encoding
属性,使用 BaseStream 参数初始化 stream
属性。 流的位置未重置。 有关附加信息,请参见 Encoding。
调用 StreamWriter 时 Dispose() , 对象 Stream 对提供 StreamWriter.Dispose 的对象调用 。
注意
使用特定文化设置编译一组字符,并使用不同的文化设置检索这些相同的字符时,这些字符可能无法解释,并可能导致引发异常。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
StreamWriter(String, Boolean, Encoding)
使用指定的编码和默认的缓冲区大小,为指定的文件初始化 StreamWriter 类的新实例。 如果该文件存在,则可以将其覆盖或向其追加。 如果该文件不存在,此构造函数将创建一个新文件。
public:
StreamWriter(System::String ^ path, bool append, System::Text::Encoding ^ encoding);
public StreamWriter (string path, bool append, System.Text.Encoding encoding);
new System.IO.StreamWriter : string * bool * System.Text.Encoding -> System.IO.StreamWriter
Public Sub New (path As String, append As Boolean, encoding As Encoding)
参数
- path
- String
要写入的完整文件路径。
- append
- Boolean
若要追加数据到该文件中,则为 true
;若要覆盖该文件,则为 false
。 如果指定的文件不存在,该参数无效,且构造函数将创建一个新文件。
- encoding
- Encoding
要使用的字符编码。
例外
访问被拒绝。
path
为 null
。
指定的路径无效(例如,它位于未映射的驱动器上)。
path
包含不正确或无效的文件名、目录名或卷标签的语法。
指定的路径和/或文件名超过了系统定义的最大长度。
调用方没有所要求的权限。
示例
下面的示例演示此构造函数。
using System;
using System.IO;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string fileName = "test.txt";
string textToAdd = "Example text in file";
using (StreamWriter writer = new StreamWriter(fileName, true, Encoding.UTF8))
{
writer.Write(textToAdd);
}
}
}
}
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim fileName As String = "test.txt"
Dim textToAdd As String = "Example text in file"
Using writer As StreamWriter = New StreamWriter(fileName, True, Encoding.UTF8)
writer.Write(textToAdd)
End Using
End Sub
End Module
注解
此构造函数使用编码 Encoding 参数初始化 属性。 有关附加信息,请参见 Encoding。
path
可以是文件名,包括 UNC 共享中通用命名约定 () 文件。
path
不需要是磁盘上存储的文件;它可以是支持通过流访问的系统的任何部分。
注意
使用特定文化设置编译一组字符,并使用不同的文化设置检索这些相同的字符时,这些字符可能无法解释,并可能导致引发异常。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
StreamWriter(String, Encoding, FileStreamOptions)
使用指定的编码并配置指定的 对象,为指定文件初始化 StreamWriter 类的新 FileStreamOptions 实例。
public:
StreamWriter(System::String ^ path, System::Text::Encoding ^ encoding, System::IO::FileStreamOptions ^ options);
public StreamWriter (string path, System.Text.Encoding encoding, System.IO.FileStreamOptions options);
new System.IO.StreamWriter : string * System.Text.Encoding * System.IO.FileStreamOptions -> System.IO.StreamWriter
Public Sub New (path As String, encoding As Encoding, options As FileStreamOptions)
参数
- path
- String
要写入的完整文件路径。
- encoding
- Encoding
要使用的字符编码。
- options
- FileStreamOptions
一个 对象,该对象指定基础 的配置选项 FileStream 。
例外
options
为 null
。
stream
不可写。
另请参阅
适用于
StreamWriter(Stream, Encoding, Int32, Boolean)
使用指定的编码和缓冲区大小,为指定的流初始化 StreamWriter 类的新实例,并可以选择保持流处于打开状态。
public:
StreamWriter(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, int bufferSize, bool leaveOpen);
public StreamWriter (System.IO.Stream stream, System.Text.Encoding encoding, int bufferSize, bool leaveOpen);
public StreamWriter (System.IO.Stream stream, System.Text.Encoding? encoding = default, int bufferSize = -1, bool leaveOpen = false);
new System.IO.StreamWriter : System.IO.Stream * System.Text.Encoding * int * bool -> System.IO.StreamWriter
Public Sub New (stream As Stream, encoding As Encoding, bufferSize As Integer, leaveOpen As Boolean)
Public Sub New (stream As Stream, Optional encoding As Encoding = Nothing, Optional bufferSize As Integer = -1, Optional leaveOpen As Boolean = false)
参数
- stream
- Stream
要写入的流。
- encoding
- Encoding
要使用的字符编码。
- bufferSize
- Int32
缓冲区大小(以字节为单位)。
- leaveOpen
- Boolean
如果在释放 StreamWriter 对象后保持流处于打开状态,则为 true
;否则为 false
。
例外
stream
或 encoding
为 null
。
bufferSize
为负数。
stream
不可写。
示例
下面的示例演示此构造函数。
using System;
using System.IO;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string fileName = "test.txt";
string textToAdd = "Example text in file";
FileStream fs = null;
try
{
fs = new FileStream(fileName, FileMode.CreateNew);
using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8, 512, false))
{
writer.Write(textToAdd);
}
}
finally
{
if (fs != null)
fs.Dispose();
}
}
}
}
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim fileName As String = "test.txt"
Dim textToAdd As String = "Example text in file"
Dim fs As FileStream = Nothing
Try
fs = New FileStream(fileName, FileMode.CreateNew)
Using writer As StreamWriter = New StreamWriter(fs, Encoding.Default, 512, False)
writer.Write(textToAdd)
End Using
Finally
If Not fs Is Nothing Then
fs.Dispose()
End If
End Try
End Sub
End Module
注解
除非您设置leaveOpen
参数true
,则StreamWriter对象调用Dispose()上提供Stream对象时StreamWriter.Dispose调用。
此构造函数使用 参数初始化 属性,然后使用 Encoding encoding
BaseStream 参数初始化 stream
属性。 流的位置未重置。 有关其他信息,请参阅 Encoding 属性。
注意
使用特定文化设置编译一组字符,并使用不同的文化设置检索这些相同的字符时,这些字符可能无法解释,并可能导致引发异常。
适用于
StreamWriter(String, Boolean, Encoding, Int32)
使用指定编码和缓冲区大小,为指定路径上的指定文件初始化 StreamWriter 类的新实例。 如果该文件存在,则可以将其覆盖或向其追加。 如果该文件不存在,此构造函数将创建一个新文件。
public:
StreamWriter(System::String ^ path, bool append, System::Text::Encoding ^ encoding, int bufferSize);
public StreamWriter (string path, bool append, System.Text.Encoding encoding, int bufferSize);
new System.IO.StreamWriter : string * bool * System.Text.Encoding * int -> System.IO.StreamWriter
Public Sub New (path As String, append As Boolean, encoding As Encoding, bufferSize As Integer)
参数
- path
- String
要写入的完整文件路径。
- append
- Boolean
若要追加数据到该文件中,则为 true
;若要覆盖该文件,则为 false
。 如果指定的文件不存在,该参数无效,且构造函数将创建一个新文件。
- encoding
- Encoding
要使用的字符编码。
- bufferSize
- Int32
缓冲区大小(以字节为单位)。
例外
path
或 encoding
为 null
。
bufferSize
为负数。
path
包含不正确或无效的文件名、目录名或卷标签的语法。
调用方没有所要求的权限。
访问被拒绝。
指定的路径无效(例如,它位于未映射的驱动器上)。
指定的路径和/或文件名超过了系统定义的最大长度。
示例
下面的示例演示此构造函数。
using System;
using System.IO;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string fileName = "test.txt";
string textToAdd = "Example text in file";
using (StreamWriter writer = new StreamWriter(fileName, true, Encoding.UTF8, 512))
{
writer.Write(textToAdd);
}
}
}
}
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim fileName As String = "test.txt"
Dim textToAdd As String = "Example text in file"
Using writer As StreamWriter = New StreamWriter(fileName, True, Encoding.UTF8, 512)
writer.Write(textToAdd)
End Using
End Sub
End Module
注解
此构造函数使用编码 Encoding 参数初始化 属性。 有关附加信息,请参见 Encoding。
path
可以是文件名,包括 UNC 共享中通用命名约定 () 文件。
path
不需要是磁盘上存储的文件;它可以是支持通过流访问的系统的任何部分。
注意
使用特定文化设置编译一组字符,并使用不同的文化设置检索这些相同的字符时,这些字符可能无法解释,并可能导致引发异常。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。