StreamWriter.Write 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将数据写入流。
重载
Write(String, Object, Object, Object) |
使用与 Format(String, Object, Object, Object) 方法相同的语义将格式化字符串写入流。 |
Write(String, Object, Object) |
使用与 方法相同的语义 Format(String, Object, Object) 将格式化字符串写入流。 |
Write(Char[], Int32, Int32) |
将字符的子数组写入流。 |
Write(String, ReadOnlySpan<Object>) | |
Write(String, Object[]) |
使用与 Format(String, Object[]) 方法相同的语义将格式化字符串写入流。 |
Write(ReadOnlySpan<Char>) |
将字符范围写流。 |
Write(String) |
将字符串写入流。 |
Write(Char[]) |
将字符数组写入流。 |
Write(Char) |
将字符写入流。 |
Write(String, Object) |
使用与 Format(String, Object) 方法相同的语义将格式化字符串写入流。 |
Write(String, Object, Object, Object)
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
使用与 Format(String, Object, Object, Object) 方法相同的语义将格式化字符串写入流。
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
复合格式字符串。
- arg0
- Object
要格式化和写入的第一个对象。
- arg1
- Object
要格式化和写入的第二个对象。
- arg2
- Object
要设置格式和写入的第三个对象。
注解
有关提供的复合格式设置功能的说明,请参阅 Write(String, Object, Object, Object) 。
适用于
Write(String, Object, Object)
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
使用与 方法相同的语义 Format(String, Object, Object) 将格式化字符串写入流。
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
复合格式字符串。
- arg0
- Object
要格式化和写入的第一个对象。
- arg1
- Object
要格式化和写入的第二个对象。
注解
有关提供的复合格式设置功能的说明,请参阅 Write(String, Object, Object) 。
适用于
Write(Char[], Int32, Int32)
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
将字符的子数组写入流。
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[]
包含要写入的数据的字符数组。
- index
- Int32
在开始读取数据时缓冲区中的字符位置。
- count
- Int32
要写入的最大字符数。
例外
buffer
为 null
。
缓冲区长度减去 index
小于 count
。
index
或 count
为负数。
出现 I/O 错误。
AutoFlush 为 true 或 StreamWriter 缓冲区已满,并且当前编写器已关闭。
AutoFlush 为 true 或 StreamWriter 缓冲区已满,并且由于 StreamWriter 位于基础固定大小流的结尾,缓冲区的内容无法写入该流。
示例
此示例将 13 个元素数组中的 8 个字符写入文件,从数组的第三个元素开始。
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.Write。
字符从 buffer
开始 index
读取,然后持续到 index
+ (count
- 1) 。 除非提前到达基础流的末尾,否则所有字符都会写入基础流。
Flush如果 为 true
,则AutoFlush会自动调用 。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
Write(String, ReadOnlySpan<Object>)
public:
override void Write(System::String ^ format, ReadOnlySpan<System::Object ^> arg);
public override void Write (string format, scoped ReadOnlySpan<object?> arg);
override this.Write : string * ReadOnlySpan<obj> -> unit
Public Overrides Sub Write (format As String, arg As ReadOnlySpan(Of Object))
参数
- format
- String
- arg
- ReadOnlySpan<Object>
适用于
Write(String, Object[])
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
使用与 Format(String, Object[]) 方法相同的语义将格式化字符串写入流。
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
复合格式字符串。
- arg
- Object[]
一个对象数组,其中包含零个或多个要设置格式和写入的对象。
注解
有关提供的复合格式设置功能的说明,请参阅 Write(String, Object[]) 。
适用于
Write(ReadOnlySpan<Char>)
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
将字符范围写流。
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>
要写入的字符范围。
适用于
Write(String)
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
将字符串写入流。
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
要写入流的字符串。 如果 value
为 null,则不写入任何内容。
例外
AutoFlush 为 true 或 StreamWriter 缓冲区已满,并且当前编写器已关闭。
AutoFlush 为 true 或 StreamWriter 缓冲区已满,并且由于 StreamWriter 位于基础固定大小流的结尾,缓冲区的内容无法写入该流。
出现 I/O 错误。
注解
此方法重写 TextWriter.Write。
指定的 String 将写入基础流,除非过早到达流的末尾。
Flush如果 为 true
,则AutoFlush会自动调用 。 如果 value
为 null
,则不写入任何条目。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
Write(Char[])
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
将字符数组写入流。
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[]
包含要写入的数据的字符数组。 如果 buffer
为 null
,则不写入任何内容。
例外
出现 I/O 错误。
AutoFlush 为 true 或 StreamWriter 缓冲区已满,并且当前编写器已关闭。
AutoFlush 为 true 或 StreamWriter 缓冲区已满,并且由于 StreamWriter 位于基础固定大小流的结尾,缓冲区的内容无法写入该流。
注解
此方法重写 TextWriter.Write。
除非过早到达流的末尾,否则指定的字符将写入基础流。 如果 AutoFlush 为 true
, Flush 则会自动调用 。
此方法提供的性能可能比 Write
() char[],``int,``int
更快,因为它要检查的参数较少。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
Write(Char)
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
将字符写入流。
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
要写入流中的字符。
例外
出现 I/O 错误。
AutoFlush 为 true 或 StreamWriter 缓冲区已满,并且当前编写器已关闭。
AutoFlush 为 true 或 StreamWriter 缓冲区已满,并且由于 StreamWriter 位于基础固定大小流的结尾,缓冲区的内容无法写入该流。
注解
此方法重写 TextWriter.Write。
指定的字符将写入基础流,除非过早到达流的末尾。 如果 AutoFlush 为 true
, Flush 则会自动调用 。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
Write(String, Object)
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
使用与 Format(String, Object) 方法相同的语义将格式化字符串写入流。
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
复合格式字符串。
- arg0
- Object
要格式化和写入的对象。
注解
有关提供的复合格式设置功能的说明,请参阅 Write(String, Object) 。
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈