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

參數

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 編碼建立,但不含位元組順序標記(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.UTF8屬性中的編碼物件所提供的行為。This behavior is different from the behavior provided by the encoding object in the Encoding.UTF8 property. 若要指定是否在不正確位元組上擲回例外狀況,請使用接受編碼物件做為參數的函式StreamWriter,例如。To specify whether an exception is thrown on invalid bytes, use a constructor that accepts an encoding object as a parameter, such as StreamWriter. BaseStream屬性會stream使用參數初始化。The BaseStream property is initialized using the stream parameter. 資料流程的位置不會重設。The position of the stream is not reset.

StreamWriter呼叫時Stream Dispose() ,物件StreamWriter.Dispose會在所提供的物件上呼叫。The 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 編碼建立,但不含位元組順序標記(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.UTF8屬性中的編碼物件所提供的行為。This 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.

安全性

FileIOPermission
用於讀取和寫入檔案。for reading and writing files. 相關聯的Read列舉:、WriteAssociated enumeration: Read, Write

另請參閱

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

參數

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 參數,以及使用 stream 參數BaseStream的屬性,來初始化屬性。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呼叫時Stream Dispose() ,物件StreamWriter.Dispose會在所提供的物件上呼叫。The 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 表示要附加資料至檔案,false 表示要覆寫檔案。true 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 編碼建立,但不含位元組順序標記(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.UTF8屬性中的編碼物件所提供的行為。This 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.

安全性

FileIOPermission
用於讀取和寫入檔案。for reading and writing files. 相關聯的Read列舉:、WriteAssociated enumeration: Read, Write

另請參閱

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

參數

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使用參數, stream以及BaseStream使用參數的屬性,初始化屬性。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呼叫時Stream Dispose() ,物件StreamWriter.Dispose會在所提供的物件上呼叫。The 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

參數

path
String

要寫入的完整檔案路徑。The complete file path to write to.

append
Boolean

true 表示要附加資料至檔案,false 表示要覆寫檔案。true 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使用 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.

安全性

FileIOPermission
用於讀取和寫入檔案。for reading and writing files. 相關聯的Read列舉:、WriteAssociated enumeration: Read, Write

另請參閱

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 = null, int bufferSize = -1, bool leaveOpen = false);
new System.IO.StreamWriter : System.IO.Stream * System.Text.Encoding * int * bool -> System.IO.StreamWriter

參數

stream
Stream

要寫入的資料流。The stream to write to.

encoding
Encoding

要使用的字元編碼。The character encoding to use.

bufferSize
Int32

緩衝區大小,以位元組為單位。The buffer size, in bytes.

leaveOpen
Boolean

true 表示在處置 StreamWriter 物件之後,將資料流保持開啟;否則為 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將參數設為trueStreamWriter.Dispose StreamWriter否則當呼叫Dispose()時,物件Stream會在所提供的物件上呼叫。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參數來初始化屬性,並使用stream參數來初始化屬性。This 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

參數

path
String

要寫入的完整檔案路徑。The complete file path to write to.

append
Boolean

true 表示要附加資料至檔案,false 表示要覆寫檔案。true 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使用 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.

安全性

FileIOPermission
用於讀取和寫入檔案。for reading and writing files. 相關聯的Read列舉:、WriteAssociated enumeration: Read, Write

另請參閱

適用於