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元素数组向文件写入8个字符,从数组的第三个元素开始。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.

适用于