StreamWriter.Write 方法

定義

寫入資料至資料流。Writes data to the stream.

多載

Write(String, Object, Object, Object)

使用與 Format(String, Object, Object, Object) 方法相同的語意,將格式化字串寫入資料流。Writes a formatted string to the stream, using the same semantics as the Format(String, Object, Object, Object) method.

Write(String, Object, Object)

使用與 Format(String, Object, Object) 方法相同的語意,將格式化字串寫入資料流。Writes a formatted string to the stream using the same semantics as the Format(String, Object, Object) method..

Write(Char[], Int32, Int32)

將字元子陣列寫入資料流。Writes a subarray of characters to the stream.

Write(String, Object[])

使用與 Format(String, Object[]) 方法相同的語意,將格式化字串寫入資料流。Writes a formatted string to the stream, using the same semantics as the Format(String, Object[]) method.

Write(ReadOnlySpan<Char>)

將字元範圍寫入資料流。Writes a character span to the stream.

Write(String)

將字串寫入資料流。Writes a string to the stream.

Write(Char[])

將字元陣列寫入資料流。Writes a character array to the stream.

Write(Char)

將一個字元寫入資料流。Writes a character to the stream.

Write(String, Object)

使用與 Format(String, Object) 方法相同的語意,將格式化字串寫入資料流。Writes a formatted string to the stream, using the same semantics as the Format(String, Object) method.

Write(String, Object, Object, Object)

使用與 Format(String, Object, Object, Object) 方法相同的語意,將格式化字串寫入資料流。Writes a formatted string to the stream, using the same semantics as the Format(String, Object, Object, Object) method.

public:
 override void Write(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1, System::Object ^ arg2);
public override void Write (string format, object? arg0, object? arg1, object? arg2);
override this.Write : string * obj * obj * obj -> unit
Public Overrides Sub Write (format As String, arg0 As Object, arg1 As Object, arg2 As Object)

參數

format
String

複合格式字串。A composite format string.

arg0
Object

第一個要格式化和寫入的物件。The first object to format and write.

arg1
Object

第二個要格式化和寫入的物件。The second object to format and write.

arg2
Object

第三個要格式化和寫入的物件。The third object to format and write.

備註

Write(String, Object, Object, Object)如需所提供複合格式功能的說明,請參閱。See Write(String, Object, Object, Object) for a description of the composite formatting capabilities offered.

適用於

Write(String, Object, Object)

使用與 Format(String, Object, Object) 方法相同的語意,將格式化字串寫入資料流。Writes a formatted string to the stream using the same semantics as the Format(String, Object, Object) method..

public:
 override void Write(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1);
public override void Write (string format, object? arg0, object? arg1);
override this.Write : string * obj * obj -> unit
Public Overrides Sub Write (format As String, arg0 As Object, arg1 As Object)

參數

format
String

複合格式字串。A composite format string.

arg0
Object

第一個要格式化和寫入的物件。The first object to format and write.

arg1
Object

第二個要格式化和寫入的物件。The second object to format and write.

備註

Write(String, Object, Object)如需所提供複合格式功能的說明,請參閱。See Write(String, Object, Object) for a description of the composite formatting capabilities offered.

適用於

Write(Char[], Int32, Int32)

將字元子陣列寫入資料流。Writes a subarray of characters to the stream.

public:
 override void Write(cli::array <char> ^ buffer, int index, int count);
public override void Write (char[] buffer, int index, int count);
override this.Write : char[] * int * int -> unit
Public Overrides Sub Write (buffer As Char(), index As Integer, count As Integer)

參數

buffer
Char[]

含有要寫入之資料的字元陣列。A character array that contains the data to write.

index
Int32

緩衝區中要開始讀取資料的字元位置。The character position in the buffer at which to start reading data.

count
Int32

要寫入的最大字元數。The maximum number of characters to write.

例外狀況

buffernullbuffer is null.

緩衝區長度減去 index 會小於 countThe buffer length minus index is less than count.

indexcount 為負。index or count is negative.

發生 I/O 錯誤。An I/O error occurs.

AutoFlush 為 True,或 StreamWriter 緩衝區已滿,且目前的寫入器已經關閉。AutoFlush is true or the StreamWriter buffer is full, and current writer is closed.

AutoFlush 為 True,或 StreamWriter 緩衝區已滿,且因為 StreamWriter 是在資料流緩衝區的結尾,所以該緩衝區的內容無法寫入至基礎的固定大小資料流。AutoFlush is true or the StreamWriter buffer is full, and the contents of the buffer cannot be written to the underlying fixed size stream because the StreamWriter is at the end the stream.

範例

此範例會從陣列的第三個元素開始,將13個元素的陣列中的八個字元寫入至檔案。This example writes eight characters from a 13-element array to a file, beginning at the third element of the array.

using namespace System;
using namespace System::IO;

int main()
{
   FileStream^ sb = gcnew FileStream( "MyFile.txt",FileMode::OpenOrCreate );
   array<Char>^b = {'a','b','c','d','e','f','g','h','i','j','k','l','m'};
   StreamWriter^ sw = gcnew StreamWriter( sb );
   sw->Write( b, 3, 8 );
   sw->Close();
}
using System;
using System.IO;

public class SWBuff
{
    public static void Main(String[] args)
    {
        FileStream sb = new FileStream("MyFile.txt", FileMode.OpenOrCreate);
        char[] b = {'a','b','c','d','e','f','g','h','i','j','k','l','m'};
        StreamWriter sw = new StreamWriter(sb);
        sw.Write(b, 3, 8);
        sw.Close();
    }
}
Imports System.IO

Public Class SWBuff

    Public Shared Sub Main()
        Dim sb As New FileStream("MyFile.txt", FileMode.OpenOrCreate)
        Dim b As Char() = {"a"c, "b"c, "c"c, "d"c, "e"c, "f"c, "g"c, _
           "h"c, "i"c, "j"c, "k"c, "l"c, "m"c}
        Dim sw As New StreamWriter(sb)
        sw.Write(b, 3, 8)
        sw.Close()
    End Sub
End Class

備註

這個方法會覆寫 TextWriter.WriteThis method overrides TextWriter.Write.

字元會從 buffer 一開始讀取 index ,並繼續進行 index + (count -1) 。The characters are read from buffer beginning at index and continuing through index + (count - 1). 除非已提前到達基礎資料流程的結尾,否則所有字元都會寫入至基礎資料流程。All characters are written to the underlying stream unless the end of the underlying stream is reached prematurely. Flush 如果為,就會自動叫用 AutoFlush trueFlush is invoked automatically if AutoFlush is true.

如需一般 i/o 工作的清單,請參閱 一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

適用於

Write(String, Object[])

使用與 Format(String, Object[]) 方法相同的語意,將格式化字串寫入資料流。Writes a formatted string to the stream, using the same semantics as the Format(String, Object[]) method.

public:
 override void Write(System::String ^ format, ... cli::array <System::Object ^> ^ arg);
public override void Write (string format, params object?[] arg);
override this.Write : string * obj[] -> unit
Public Overrides Sub Write (format As String, ParamArray arg As Object())

參數

format
String

複合格式字串。A composite format string.

arg
Object[]

物件陣列,包含零或多個要格式化和寫入的物件。An object array that contains zero or more objects to format and write.

備註

Write(String, Object[])如需所提供複合格式功能的說明,請參閱。See Write(String, Object[]) for a description of the composite formatting capabilities offered.

適用於

Write(ReadOnlySpan<Char>)

將字元範圍寫入資料流。Writes a character span to the stream.

public:
 override void Write(ReadOnlySpan<char> buffer);
public override void Write (ReadOnlySpan<char> buffer);
override this.Write : ReadOnlySpan<char> -> unit
Public Overrides Sub Write (buffer As ReadOnlySpan(Of Char))

參數

buffer
ReadOnlySpan<Char>

要寫入的字元範圍。The character span to write.

適用於

Write(String)

將字串寫入資料流。Writes a string to the stream.

public:
 override void Write(System::String ^ value);
public override void Write (string value);
public override void Write (string? value);
override this.Write : string -> unit
Public Overrides Sub Write (value As String)

參數

value
String

要寫入資料流的字串。The string to write to the stream. 如果 value 是 Null,則不會寫入任何資料。If value is null, nothing is written.

例外狀況

AutoFlush 為 True,或 StreamWriter 緩衝區已滿,且目前的寫入器已經關閉。AutoFlush is true or the StreamWriter buffer is full, and current writer is closed.

AutoFlush 為 True,或 StreamWriter 緩衝區已滿,且因為 StreamWriter 是在資料流緩衝區的結尾,所以該緩衝區的內容無法寫入至基礎的固定大小資料流。AutoFlush is true or the StreamWriter buffer is full, and the contents of the buffer cannot be written to the underlying fixed size stream because the StreamWriter is at the end the stream.

發生 I/O 錯誤。An I/O error occurs.

備註

這個方法會覆寫 TextWriter.WriteThis method overrides TextWriter.Write.

String除非過早到達資料流程的結尾,否則會將指定的寫入至基礎資料流程。The specified String is written to the underlying stream unless the end of the stream is reached prematurely.

Flush 如果為,就會自動叫用 AutoFlush trueFlush is invoked automatically if AutoFlush is true. 如果 valuenull ,則不會寫入任何專案。If value is null, no entries are written.

如需一般 i/o 工作的清單,請參閱 一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

適用於

Write(Char[])

將字元陣列寫入資料流。Writes a character array to the stream.

public:
 override void Write(cli::array <char> ^ buffer);
public override void Write (char[] buffer);
public override void Write (char[]? buffer);
override this.Write : char[] -> unit
Public Overrides Sub Write (buffer As Char())

參數

buffer
Char[]

含有要寫入之資料的字元陣列。A character array containing the data to write. 如果 buffernull,則不寫入任何資料。If buffer is null, nothing is written.

例外狀況

發生 I/O 錯誤。An I/O error occurs.

AutoFlush 為 True,或 StreamWriter 緩衝區已滿,且目前的寫入器已經關閉。AutoFlush is true or the StreamWriter buffer is full, and current writer is closed.

AutoFlush 為 True,或 StreamWriter 緩衝區已滿,且因為 StreamWriter 是在資料流緩衝區的結尾,所以該緩衝區的內容無法寫入至基礎的固定大小資料流。AutoFlush is true or the StreamWriter buffer is full, and the contents of the buffer cannot be written to the underlying fixed size stream because the StreamWriter is at the end the stream.

備註

這個方法會覆寫 TextWriter.WriteThis method overrides TextWriter.Write.

除非過早到達資料流程的結尾,否則指定的字元會寫入至基礎資料流程。The specified characters are written to the underlying stream unless the end of the stream is reached prematurely. 如果 AutoFlushtrueFlush 就會自動叫用。If AutoFlush is true, Flush is invoked automatically.

這個方法可能會比 () 提供更快的效能 Write char[],``int,``int ,因為它有較少的檢查引數。This method might provide faster performance than Write (char[],``int,``int) because it has fewer arguments to check.

如需一般 i/o 工作的清單,請參閱 一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

適用於

Write(Char)

將一個字元寫入資料流。Writes a character to the stream.

public:
 override void Write(char value);
public override void Write (char value);
override this.Write : char -> unit
Public Overrides Sub Write (value As Char)

參數

value
Char

要寫入資料流的字元。The character to write to the stream.

例外狀況

發生 I/O 錯誤。An I/O error occurs.

AutoFlush 為 True,或 StreamWriter 緩衝區已滿,且目前的寫入器已經關閉。AutoFlush is true or the StreamWriter buffer is full, and current writer is closed.

AutoFlush 為 True,或 StreamWriter 緩衝區已滿,且因為 StreamWriter 是在資料流緩衝區的結尾,所以該緩衝區的內容無法寫入至基礎的固定大小資料流。AutoFlush is true or the StreamWriter buffer is full, and the contents of the buffer cannot be written to the underlying fixed size stream because the StreamWriter is at the end the stream.

備註

這個方法會覆寫 TextWriter.WriteThis method overrides TextWriter.Write.

除非過早到達資料流程的結尾,否則指定的字元會寫入至基礎資料流程。The specified character is written to the underlying stream unless the end of the stream is reached prematurely. 如果 AutoFlushtrueFlush 就會自動叫用。If AutoFlush is true, Flush is invoked automatically.

如需一般 i/o 工作的清單,請參閱 一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

適用於

Write(String, Object)

使用與 Format(String, Object) 方法相同的語意,將格式化字串寫入資料流。Writes a formatted string to the stream, using the same semantics as the Format(String, Object) method.

public:
 override void Write(System::String ^ format, System::Object ^ arg0);
public override void Write (string format, object? arg0);
override this.Write : string * obj -> unit
Public Overrides Sub Write (format As String, arg0 As Object)

參數

format
String

複合格式字串。A composite format string.

arg0
Object

要格式化及寫入的物件。The object to format and write.

備註

Write(String, Object)如需所提供複合格式功能的說明,請參閱。See Write(String, Object) for a description of the composite formatting capabilities offered.

適用於