StreamWriter 构造函数

定义

初始化 StreamWriter 类的新实例。Initializes a new instance of the StreamWriter class.

重载

StreamWriter(Stream)

使用 UTF-8 编码及默认的缓冲区大小,为指定的流初始化 StreamWriter 类的新实例。Initializes a new instance of the StreamWriter class for the specified stream by using UTF-8 encoding and the default buffer size.

StreamWriter(String)

用默认编码和缓冲区大小,为指定的文件初始化 StreamWriter 类的一个新实例。Initializes a new instance of the StreamWriter class for the specified file by using the default encoding and buffer size.

StreamWriter(Stream, Encoding)

使用指定的编码及默认的缓冲区大小,为指定的流初始化 StreamWriter 类的新实例。Initializes a new instance of the StreamWriter class for the specified stream by using the specified encoding and the default buffer size.

StreamWriter(String, Boolean)

用默认编码和缓冲区大小,为指定的文件初始化 StreamWriter 类的一个新实例。Initializes a new instance of the StreamWriter class for the specified file by using the default encoding and buffer size. 如果该文件存在,则可以将其覆盖或向其追加。If the file exists, it can be either overwritten or appended to. 如果该文件不存在,此构造函数将创建一个新文件。If the file does not exist, this constructor creates a new file.

StreamWriter(Stream, Encoding, Int32)

使用指定的编码及缓冲区大小,为指定的流初始化 StreamWriter 类的新实例。Initializes a new instance of the StreamWriter class for the specified stream by using the specified encoding and buffer size.

StreamWriter(String, Boolean, Encoding)

使用指定的编码和默认的缓冲区大小,为指定的文件初始化 StreamWriter 类的新实例。Initializes a new instance of the StreamWriter class for the specified file by using the specified encoding and default buffer size. 如果该文件存在,则可以将其覆盖或向其追加。If the file exists, it can be either overwritten or appended to. 如果该文件不存在,此构造函数将创建一个新文件。If the file does not exist, this constructor creates a new file.

StreamWriter(Stream, Encoding, Int32, Boolean)

使用指定的编码和缓冲区大小,为指定的流初始化 StreamWriter 类的新实例,并可以选择保持流处于打开状态。Initializes a new instance of the StreamWriter class for the specified stream by using the specified encoding and buffer size, and optionally leaves the stream open.

StreamWriter(String, Boolean, Encoding, Int32)

使用指定编码和缓冲区大小,为指定路径上的指定文件初始化 StreamWriter 类的新实例。Initializes a new instance of the StreamWriter class for the specified file on the specified path, using the specified encoding and buffer size. 如果该文件存在,则可以将其覆盖或向其追加。If the file exists, it can be either overwritten or appended to. 如果该文件不存在,此构造函数将创建一个新文件。If the file does not exist, this constructor creates a new file.

StreamWriter(Stream)

使用 UTF-8 编码及默认的缓冲区大小,为指定的流初始化 StreamWriter 类的新实例。Initializes a new instance of the StreamWriter class for the specified stream by using UTF-8 encoding and the default buffer size.

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

要写入的流。The stream to write to.

例外

stream 不可写。stream is not writable.

streamnullstream is null.

示例

下面的代码示例演示了此构造函数。The following code example demonstrates this constructor.

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 方法返回一个空字节数组。This constructor creates a StreamWriter with UTF-8 encoding without a Byte-Order Mark (BOM), so its GetPreamble method returns an empty byte array. 此构造函数的默认 UTF-8 编码对无效字节引发异常。The default UTF-8 encoding for this constructor throws an exception on invalid bytes. 此行为不同于属性中的编码对象提供的行为 Encoding.UTF8This behavior is different from the behavior provided by the encoding object in the Encoding.UTF8 property. 若要指定是否在无效字节上引发异常,请使用接受编码对象作为参数的构造函数,例如 StreamWriterTo specify whether an exception is thrown on invalid bytes, use a constructor that accepts an encoding object as a parameter, such as StreamWriter. BaseStream属性使用参数进行初始化 streamThe BaseStream property is initialized using the stream parameter. 不重置流的位置。The position of the stream is not reset.

StreamWriter调用时,对象对 Dispose() 提供的 Stream 对象调用 StreamWriter.DisposeThe StreamWriter object calls Dispose() on the provided Stream object when StreamWriter.Dispose is called.

注意

使用特定的区域性设置编译一组字符并使用不同的区域性设置检索这些相同的字符时,这些字符可能无法可解释,并且可能会导致引发异常。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable and could cause an exception to be thrown.

有关常见 i/o 任务的列表,请参阅 常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

适用于

StreamWriter(String)

用默认编码和缓冲区大小,为指定的文件初始化 StreamWriter 类的一个新实例。Initializes a new instance of the StreamWriter class for the specified file by using the default encoding and buffer size.

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

要写入的完整文件路径。The complete file path to write to. path 可以是一个文件名。path can be a file name.

例外

访问被拒绝。Access is denied.

path 为空字符串 ("")。path is an empty string ("").

- 或 --or- path 包含系统设备的名称(com1、com2 等等)。path contains the name of a system device (com1, com2, and so on).

pathnullpath is null.

指定的路径无效(例如,它位于未映射的驱动器上)。The specified path is invalid (for example, it is on an unmapped drive).

指定的路径和/或文件名超过了系统定义的最大长度。The specified path, file name, or both exceed the system-defined maximum length.

path 包含不正确或无效的文件名、目录名或卷标签的语法。path includes an incorrect or invalid syntax for file name, directory name, or volume label syntax.

调用方没有所要求的权限。The caller does not have the required permission.

示例

下面的代码示例演示了此构造函数。The following code example demonstrates this constructor.

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 方法返回一个空字节数组。This constructor creates a StreamWriter with UTF-8 encoding without a Byte-Order Mark (BOM), so its GetPreamble method returns an empty byte array. 此构造函数的默认 UTF-8 编码对无效字节引发异常。The default UTF-8 encoding for this constructor throws an exception on invalid bytes. 此行为不同于属性中的编码对象提供的行为 Encoding.UTF8This behavior is different from the behavior provided by the encoding object in the Encoding.UTF8 property. 若要指定一个 BOM 并确定无效字节是否引发了异常,请使用接受编码对象作为参数的构造函数,例如 StreamWriter(String, Boolean, Encoding)To specify a BOM and determine whether an exception is thrown on invalid bytes, use a constructor that accepts an encoding object as a parameter, such as StreamWriter(String, Boolean, Encoding).

path参数可以是文件名,包括通用命名约定中的文件 (UNC) 共享。The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share. 如果该文件存在,则会被覆盖;否则,将创建新的文件。If the file exists, it is overwritten; otherwise, a new file is created.

path参数不需要是存储在磁盘上的文件; 它可以是系统的任何支持使用流进行访问的部分。The path parameter is not required to be a file stored on disk; it can be any part of a system that supports access using streams.

注意

使用特定的区域性设置编译一组字符并使用不同的区域性设置检索这些相同的字符时,这些字符可能无法可解释,并且可能会导致引发异常。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable and could cause an exception to be thrown.

有关常见 i/o 任务的列表,请参阅 常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

适用于

StreamWriter(Stream, Encoding)

使用指定的编码及默认的缓冲区大小,为指定的流初始化 StreamWriter 类的新实例。Initializes a new instance of the StreamWriter class for the specified stream by using the specified encoding and the default buffer size.

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

要写入的流。The stream to write to.

encoding
Encoding

要使用的字符编码。The character encoding to use.

例外

streamencodingnullstream or encoding is null.

stream 不可写。stream is not writable.

示例

下面的示例演示了此构造函数。The following example demonstrates this constructor.

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 参数的属性初始化属性。This constructor initializes the Encoding property using the encoding parameter, and the BaseStream property using the stream parameter. 不重置流的位置。The position of the stream is not reset. 有关附加信息,请参见 EncodingFor additional information, see Encoding.

StreamWriter调用时,对象对 Dispose() 提供的 Stream 对象调用 StreamWriter.DisposeThe StreamWriter object calls Dispose() on the provided Stream object when StreamWriter.Dispose is called.

注意

使用特定的区域性设置编译一组字符并使用不同的区域性设置检索这些相同的字符时,这些字符可能不是可解释,并且可能会导致引发异常。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

有关常见 i/o 任务的列表,请参阅 常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

另请参阅

适用于

StreamWriter(String, Boolean)

用默认编码和缓冲区大小,为指定的文件初始化 StreamWriter 类的一个新实例。Initializes a new instance of the StreamWriter class for the specified file by using the default encoding and buffer size. 如果该文件存在,则可以将其覆盖或向其追加。If the file exists, it can be either overwritten or appended to. 如果该文件不存在,此构造函数将创建一个新文件。If the file does not exist, this constructor creates a new file.

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

要写入的完整文件路径。The complete file path to write to.

append
Boolean

若要追加数据到该文件中,则为 true;若要覆盖该文件,则为 falsetrue to append data to the file; false to overwrite the file. 如果指定的文件不存在,该参数无效,且构造函数将创建一个新文件。If the specified file does not exist, this parameter has no effect, and the constructor creates a new file.

例外

访问被拒绝。Access is denied.

path 为空。path is empty.

- 或 --or- path 包含系统设备的名称(com1、com2 等等)。path contains the name of a system device (com1, com2, and so on).

pathnullpath is null.

指定的路径无效(例如,它位于未映射的驱动器上)。The specified path is invalid (for example, it is on an unmapped drive).

path 包含不正确或无效的文件名、目录名或卷标签的语法。path includes an incorrect or invalid syntax for file name, directory name, or volume label syntax.

指定的路径和/或文件名超过了系统定义的最大长度。The specified path, file name, or both exceed the system-defined maximum length.

调用方没有所要求的权限。The caller does not have the required permission.

示例

下面的代码示例演示了此构造函数。The following code example demonstrates this constructor.

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 方法返回一个空字节数组。This constructor creates a StreamWriter with UTF-8 encoding without a Byte-Order Mark (BOM), so its GetPreamble method returns an empty byte array. 此构造函数的默认 UTF-8 编码对无效字节引发异常。The default UTF-8 encoding for this constructor throws an exception on invalid bytes. 此行为不同于属性中的编码对象提供的行为 Encoding.UTF8This behavior is different from the behavior provided by the encoding object in the Encoding.UTF8 property. 若要指定一个 BOM 并确定无效字节是否引发了异常,请使用接受编码对象作为参数的构造函数,例如 StreamWriter(String, Boolean, Encoding)To specify a BOM and determine whether an exception is thrown on invalid bytes, use a constructor that accepts an encoding object as a parameter, such as StreamWriter(String, Boolean, Encoding).

path参数可以是文件名,包括通用命名约定中的文件 (UNC) 共享。The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.

path参数不需要是存储在磁盘上的文件; 它可以是系统的任何支持使用流进行访问的部分。The path parameter is not required to be a file stored on disk; it can be any part of a system that supports access using streams.

注意

使用特定的区域性设置编译一组字符并使用不同的区域性设置检索这些相同的字符时,这些字符可能不是可解释,并且可能会导致引发异常。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

有关常见 i/o 任务的列表,请参阅 常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

适用于

StreamWriter(Stream, Encoding, Int32)

使用指定的编码及缓冲区大小,为指定的流初始化 StreamWriter 类的新实例。Initializes a new instance of the StreamWriter class for the specified stream by using the specified encoding and buffer size.

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

要写入的流。The stream to write to.

encoding
Encoding

要使用的字符编码。The character encoding to use.

bufferSize
Int32

缓冲区大小(以字节为单位)。The buffer size, in bytes.

例外

streamencodingnullstream or encoding is null.

bufferSize 为负数。bufferSize is negative.

stream 不可写。stream is not writable.

示例

下面的示例演示了此构造函数。The following example demonstrates this constructor.

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 streamThis constructor initializes the Encoding property using the encoding parameter, and the BaseStream property using the stream parameter. 不重置流的位置。The position of the stream is not reset. 有关附加信息,请参见 EncodingFor additional information, see Encoding.

StreamWriter调用时,对象对 Dispose() 提供的 Stream 对象调用 StreamWriter.DisposeThe StreamWriter object calls Dispose() on the provided Stream object when StreamWriter.Dispose is called.

注意

使用特定的区域性设置编译一组字符并使用不同的区域性设置检索这些相同的字符时,这些字符可能不是可解释,并且可能会导致引发异常。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

有关常见 i/o 任务的列表,请参阅 常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

另请参阅

适用于

StreamWriter(String, Boolean, Encoding)

使用指定的编码和默认的缓冲区大小,为指定的文件初始化 StreamWriter 类的新实例。Initializes a new instance of the StreamWriter class for the specified file by using the specified encoding and default buffer size. 如果该文件存在,则可以将其覆盖或向其追加。If the file exists, it can be either overwritten or appended to. 如果该文件不存在,此构造函数将创建一个新文件。If the file does not exist, this constructor creates a new file.

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

要写入的完整文件路径。The complete file path to write to.

append
Boolean

若要追加数据到该文件中,则为 true;若要覆盖该文件,则为 falsetrue to append data to the file; false to overwrite the file. 如果指定的文件不存在,该参数无效,且构造函数将创建一个新文件。If the specified file does not exist, this parameter has no effect, and the constructor creates a new file.

encoding
Encoding

要使用的字符编码。The character encoding to use.

例外

访问被拒绝。Access is denied.

path 为空。path is empty.

- 或 --or- path 包含系统设备的名称(com1、com2 等等)。path contains the name of a system device (com1, com2, and so on).

pathnullpath is null.

指定的路径无效(例如,它位于未映射的驱动器上)。The specified path is invalid (for example, it is on an unmapped drive).

path 包含不正确或无效的文件名、目录名或卷标签的语法。path includes an incorrect or invalid syntax for file name, directory name, or volume label syntax.

指定的路径和/或文件名超过了系统定义的最大长度。The specified path, file name, or both exceed the system-defined maximum length.

调用方没有所要求的权限。The caller does not have the required permission.

示例

下面的示例演示了此构造函数。The following example demonstrates this constructor.

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 使用编码参数初始化属性。This constructor initializes the Encoding property using the encoding parameter. 有关附加信息,请参见 EncodingFor additional information, see Encoding.

path 可以是文件名,包括通用命名约定中的文件 (UNC) 共享。path can be a file name, including a file on a Universal Naming Convention (UNC) share.

path 不需要是存储在磁盘上的文件;它可以是系统中支持通过流进行访问的任何部分。path is not required to be a file stored on disk; it can be any part of a system that supports access via streams.

注意

使用特定的区域性设置编译一组字符并使用不同的区域性设置检索这些相同的字符时,这些字符可能不是可解释,并且可能会导致引发异常。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

有关常见 i/o 任务的列表,请参阅 常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

另请参阅

适用于

StreamWriter(Stream, Encoding, Int32, Boolean)

使用指定的编码和缓冲区大小,为指定的流初始化 StreamWriter 类的新实例,并可以选择保持流处于打开状态。Initializes a new instance of the StreamWriter class for the specified stream by using the specified encoding and buffer size, and optionally leaves the stream open.

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

要写入的流。The stream to write to.

encoding
Encoding

要使用的字符编码。The character encoding to use.

bufferSize
Int32

缓冲区大小(以字节为单位)。The buffer size, in bytes.

leaveOpen
Boolean

如果在释放 StreamWriter 对象后保持流处于打开状态,则为 true;否则为 falsetrue to leave the stream open after the StreamWriter object is disposed; otherwise, false.

例外

streamencodingnullstream or encoding is null.

bufferSize 为负数。bufferSize is negative.

stream 不可写。stream is not writable.

示例

下面的示例演示了此构造函数。The following example demonstrates this constructor.

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调用。Unless you set the leaveOpen parameter to true, the StreamWriter object calls Dispose() on the provided Stream object when StreamWriter.Dispose is called.

此构造函数 Encoding 通过使用参数来初始化属性 encoding ,并 BaseStream 使用参数来初始化属性 streamThis constructor initializes the Encoding property by using the encoding parameter, and initializes the BaseStream property by using the stream parameter. 不重置流的位置。The position of the stream is not reset. 有关其他信息,请参见 Encoding 属性。For additional information, see the Encoding property.

注意

使用特定的区域性设置编译一组字符并使用不同的区域性设置检索这些相同的字符时,这些字符可能不是可解释,并且可能会导致引发异常。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

适用于

StreamWriter(String, Boolean, Encoding, Int32)

使用指定编码和缓冲区大小,为指定路径上的指定文件初始化 StreamWriter 类的新实例。Initializes a new instance of the StreamWriter class for the specified file on the specified path, using the specified encoding and buffer size. 如果该文件存在,则可以将其覆盖或向其追加。If the file exists, it can be either overwritten or appended to. 如果该文件不存在,此构造函数将创建一个新文件。If the file does not exist, this constructor creates a new file.

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

要写入的完整文件路径。The complete file path to write to.

append
Boolean

若要追加数据到该文件中,则为 true;若要覆盖该文件,则为 falsetrue to append data to the file; false to overwrite the file. 如果指定的文件不存在,该参数无效,且构造函数将创建一个新文件。If the specified file does not exist, this parameter has no effect, and the constructor creates a new file.

encoding
Encoding

要使用的字符编码。The character encoding to use.

bufferSize
Int32

缓冲区大小(以字节为单位)。The buffer size, in bytes.

例外

path 为空字符串 ("")。path is an empty string ("").

- 或 --or- path 包含系统设备的名称(com1、com2 等等)。path contains the name of a system device (com1, com2, and so on).

pathencodingnullpath or encoding is null.

bufferSize 为负数。bufferSize is negative.

path 包含不正确或无效的文件名、目录名或卷标签的语法。path includes an incorrect or invalid syntax for file name, directory name, or volume label syntax.

调用方没有所要求的权限。The caller does not have the required permission.

访问被拒绝。Access is denied.

指定的路径无效(例如,它位于未映射的驱动器上)。The specified path is invalid (for example, it is on an unmapped drive).

指定的路径和/或文件名超过了系统定义的最大长度。The specified path, file name, or both exceed the system-defined maximum length.

示例

下面的示例演示了此构造函数。The following example demonstrates this constructor.

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 使用编码参数初始化属性。This constructor initializes the Encoding property using the encoding parameter. 有关附加信息,请参见 EncodingFor additional information, see Encoding.

path 可以是文件名,包括通用命名约定中的文件 (UNC) 共享。path can be a file name, including a file on a Universal Naming Convention (UNC) share.

path 不需要是存储在磁盘上的文件;它可以是系统中支持通过流进行访问的任何部分。path is not required to be a file stored on disk; it can be any part of a system that supports access via streams.

注意

使用特定的区域性设置编译一组字符并使用不同的区域性设置检索这些相同的字符时,这些字符可能不是可解释,并且可能会导致引发异常。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

有关常见 i/o 任务的列表,请参阅 常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

另请参阅

适用于