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
這些物件,以便正確處置 Unmanaged 資源。 當使用物件的程式碼完成時,語句 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) |
使用指定的編碼方式,並使用 FileStreamOptions 指定的 物件來初始化 類別的新實例 StreamWriter 。 |
StreamWriter(String, FileStreamOptions) |
使用預設編碼方式,並使用指定的 FileStreamOptions 物件來初始化 類別的新實例 StreamWriter 。 |
欄位
CoreNewLine |
儲存這個 |
Null |
提供 |
屬性
AutoFlush |
取得或設定值,指出 StreamWriter 在每次呼叫 Write(Char) 之後,是否要將其緩衝區清除到基礎資料流。 |
BaseStream |
取得以備份存放區作介面的基礎資料流。 |
Encoding |
取得寫入輸出的 Encoding。 |
FormatProvider |
取得控制格式設定的物件。 (繼承來源 TextWriter) |
NewLine |
取得或設定目前 |
方法
明確介面實作
IDisposable.Dispose() |
如需這個成員的說明,請參閱 Dispose()。 (繼承來源 TextWriter) |
擴充方法
ConfigureAwait(IAsyncDisposable, Boolean) |
設定如何執行從非同步可處置項目傳回的工作 await。 |