TextWriter.Write 方法

定义

将数据写入文本流。Writes data to a text stream.

重载

Write(String, Object, Object)

使用与 Format(String, Object, Object) 方法相同的语义将格式化字符串写入文本流。Writes a formatted string to the text stream using the same semantics as the Format(String, Object, Object) method.

Write(Char[], Int32, Int32)

将字符的子数组写入文本流。Writes a subarray of characters to the text stream.

Write(String, Object[])

使用与 Format(String, Object[]) 方法相同的语义将格式化字符串写入文本流。Writes a formatted string to the text stream, using the same semantics as the Format(String, Object[]) method.

Write(String, Object)

使用与 Format(String, Object) 方法相同的语义将格式化字符串写入文本流。Writes a formatted string to the text stream, using the same semantics as the Format(String, Object) method.

Write(UInt64)

将 8 字节无符号整数的文本表示形式写入文本流。Writes the text representation of an 8-byte unsigned integer to the text stream.

Write(UInt32)

将 4 字节无符号整数的文本表示形式写入文本流。Writes the text representation of a 4-byte unsigned integer to the text stream.

Write(StringBuilder)

将字符串生成器写入文本流。Writes a string builder to the text stream.

Write(String)

将字符串写入文本流。Writes a string to the text stream.

Write(Single)

将 4 字节浮点值的文本表示形式写入文本流。Writes the text representation of a 4-byte floating-point value to the text stream.

Write(String, Object, Object, Object)

使用与 Format(String, Object, Object, Object) 方法相同的语义将格式化字符串写入文本流。Writes a formatted string to the text stream, using the same semantics as the Format(String, Object, Object, Object) method.

Write(Object)

通过在对象上调用 ToString 方法将此对象的文本表示形式写入文本流。Writes the text representation of an object to the text stream by calling the ToString method on that object.

Write(Int64)

将 8 字节带符号整数的文本表示形式写入文本流。Writes the text representation of an 8-byte signed integer to the text stream.

Write(Int32)

将 4 字节带符号整数的文本表示形式写入文本流。Writes the text representation of a 4-byte signed integer to the text stream.

Write(Double)

将 8 字节浮点值的文本表示形式写入文本流。Writes the text representation of an 8-byte floating-point value to the text stream.

Write(Decimal)

将小数值的文本表示形式写入文本流。Writes the text representation of a decimal value to the text stream.

Write(Char[])

将字符数组写入文本流。Writes a character array to the text stream.

Write(Char)

将字符写入文本流。Writes a character to the text stream.

Write(Boolean)

Boolean 值的文本表示形式写入文本流。Writes the text representation of a Boolean value to the text stream.

Write(ReadOnlySpan<Char>)

将字符范围写入文本流。Writes a character span to the text stream.

Write(String, Object, Object)

使用与 Format(String, Object, Object) 方法相同的语义将格式化字符串写入文本流。Writes a formatted string to the text stream using the same semantics as the Format(String, Object, Object) method.

public:
 virtual void Write(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1);
public virtual void Write (string format, object arg0, object arg1);
abstract member Write : string * obj * obj -> unit
override this.Write : string * obj * obj -> unit
Public Overridable 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.

异常

formatnullformat is null.

TextWriter 是关闭的。The TextWriter is closed.

出现 I/O 错误。An I/O error occurs.

format 不是有效的复合格式字符串。format is not a valid composite format string.

-or- 格式项的索引小于 0 (0) 或大于或等于要设置格式的对象数 (用于该方法重载,为二)。The index of a format item is less than 0 (zero) or greater than or equal to the number of objects to be formatted (which, for this method overload, is two).

注解

此方法使用复合格式设置将对象的值转换为其字符串表示形式,并将该表示形式嵌入字符串。This method uses composite formatting to convert the value of an object to its string representation and to embed that representation in a string. .NET 提供广泛的格式支持,以下格式设置主题对此进行了更详细的介绍:.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

format参数包含与零个或多个索引占位符(称为格式项)混合的零个或多个文本的运行,这些占位符与此方法的参数列表中的对象相对应。The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to an object in the parameter list of this method. 格式设置过程将每个格式项替换为相应对象的值的字符串表示形式。The formatting process replaces each format item with the string representation of the value of the corresponding object.

格式项的语法如下所示:The syntax of a format item is as follows:

{index[,length] [:格式字符串]}{index[,length][:formatString]}

括号中的元素是可选的。Elements in square brackets are optional. 下表对每个元素进行了描述。The following table describes each element. 有关复合格式设置功能的详细信息(包括格式项的语法),请参阅复合格式设置For more information about the composite formatting feature, including the syntax of a format item, see Composite Formatting.

元素Element 说明Description
索引index 要设置格式的对象的参数列表中的从零开始的位置。The zero-based position in the parameter list of the object to be formatted. 如果由index指定的对象为null,则会将格式String.Empty项替换为。If the object specified by index is null, the format item is replaced by String.Empty. 由于此重载在其参数列表中有两个对象,因此index的值必须始终为0或1。Because this overload has two objects in its parameter list, the value of index must always be 0 or 1. 如果索引位置中没有参数, FormatException则会引发。If there is no parameter in the index position, a FormatException is thrown.
length,length 参数的字符串表示形式中的最小字符数。The minimum number of characters in the string representation of the parameter. 如果为正,则参数为右对齐; 否则为。如果为负,则为左对齐。If positive, the parameter is right-aligned; if negative, it is left-aligned.
:formatString:formatString 要设置格式的对象支持的标准或自定义格式字符串。A standard or custom format string that is supported by the object to be formatted. 格式字符串的可能值与对象的ToString(string format)方法所支持的值相同。Possible values for formatString are the same as the values supported by the object's ToString(string format) method. 如果未指定 "格式字符串" 并且要设置其格式的IFormattable对象实现null接口, format则将作为用作IFormattable.ToString格式字符串的参数的值传递。If formatString is not specified and the object to be formatted implements the IFormattable interface, null is passed as the value of the format parameter that is used as the IFormattable.ToString format string.

需要 "{" 和 "}" 等前导和尾随大括号。The leading and trailing brace characters, "{" and "}", are required. 若要在中format指定单个文本大括号字符,请指定两个前导或尾随大括号字符,即 "{{" 或 "}}"。To specify a single literal brace character in format, specify two leading or trailing brace characters; that is, "{{" or "}}".

此方法不会在指定的字符串中搜索各个换行符(十六进制0x000a)并将其替换TextWriter.NewLine为。This method does not search the specified string for individual newline characters (hexadecimal 0x000a) and replace them with TextWriter.NewLine.

如果在格式字符串中未引用指定的对象,则会将其忽略。If a specified object is not referenced in the format string, it is ignored.

有关常见 i/o 任务的列表, 请参阅常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

另请参阅

Write(Char[], Int32, Int32)

将字符的子数组写入文本流。Writes a subarray of characters to the text stream.

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

参数

buffer
Char[]

要从中写出数据的字符数组。The character array to write data from.

index
Int32

在开始接收数据时缓存中的字符位置。The character position in the buffer at which to start retrieving data.

count
Int32

要写入的字符数。The number of characters to write.

异常

缓冲区长度减去 index 小于 countThe buffer length minus index is less than count.

buffer 参数为 nullThe buffer parameter is null.

indexcount 为负数。index or count is negative.

TextWriter 是关闭的。The TextWriter is closed.

出现 I/O 错误。An I/O error occurs.

注解

此方法将数据count从位置buffer index开始的字符TextWriter数组写入到此中。This method will write count characters of data into this TextWriter from the buffer character array starting at position index.

此重载Write(Char[])等效于和( buffer index index )之间的每个字符的重载 + 。countThis overload is equivalent to the Write(Char[]) overload for each character in buffer between index and (index + count).

有关常见 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 text stream, using the same semantics as the Format(String, Object[]) method.

public:
 virtual void Write(System::String ^ format, ... cli::array <System::Object ^> ^ arg);
public virtual void Write (string format, params object[] arg);
abstract member Write : string * obj[] -> unit
override this.Write : string * obj[] -> unit
Public Overridable 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.

异常

formatargnullformat or arg is null.

TextWriter 是关闭的。The TextWriter is closed.

出现 I/O 错误。An I/O error occurs.

format 不是有效的复合格式字符串。format is not a valid composite format string.

-or- 格式项的索引小于 0(零),或者大于或等于 arg 数组的长度。The index of a format item is less than 0 (zero), or greater than or equal to the length of the arg array.

注解

此方法使用复合格式设置将对象的值转换为其字符串表示形式,并将该表示形式嵌入字符串。This method uses composite formatting to convert the value of an object to its string representation and to embed that representation in a string. .NET 提供广泛的格式支持,以下格式设置主题对此进行了更详细的介绍:.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

format参数包含与零个或多个索引占位符(称为格式项)混合的零个或多个文本的运行,这些占位符与此方法的参数列表中的对象相对应。The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to an object in the parameter list of this method. 格式设置过程将每个格式项替换为相应对象的值的字符串表示形式。The formatting process replaces each format item with the string representation of the value of the corresponding object.

格式项的语法如下所示:The syntax of a format item is as follows:

{index[,length] [:格式字符串]}{index[,length][:formatString]}

括号中的元素是可选的。Elements in square brackets are optional. 下表对每个元素进行了描述。The following table describes each element. 有关复合格式设置功能的详细信息(包括格式项的语法),请参阅复合格式设置For more information about the composite formatting feature, including the syntax of a format item, see Composite Formatting.

元素Element 说明Description
索引index 要设置格式的对象的参数列表中的从零开始的位置。The zero-based position in the parameter list of the object to be formatted. 如果由index指定的对象为null,则会将格式String.Empty项替换为。If the object specified by index is null, the format item is replaced by String.Empty. 由于此重载在其参数列表中具有数组,因此index的值必须始终小于数组的长度。Because this overload has an array in its parameter list, the value of index must always be less than the length of the array. 如果索引位置中没有参数, FormatException则会引发。If there is no parameter in the index position, a FormatException is thrown.
length,length 参数的字符串表示形式中的最小字符数。The minimum number of characters in the string representation of the parameter. 如果为正,则参数为右对齐; 否则为。如果为负,则为左对齐。If positive, the parameter is right-aligned; if negative, it is left-aligned.
:formatString:formatString 要设置格式的对象支持的标准或自定义格式字符串。A standard or custom format string that is supported by the object to be formatted. 格式字符串的可能值与对象的ToString(string format)方法所支持的值相同。Possible values for formatString are the same as the values supported by the object's ToString(string format) method. 如果未指定 "格式字符串" 并且要设置其格式的IFormattable对象实现null接口, format则将作为用作IFormattable.ToString格式字符串的参数的值传递。If formatString is not specified and the object to be formatted implements the IFormattable interface, null is passed as the value of the format parameter that is used as the IFormattable.ToString format string.

需要 "{" 和 "}" 等前导和尾随大括号。The leading and trailing brace characters, "{" and "}", are required. 若要在中format指定单个文本大括号字符,请指定两个前导或尾随大括号字符,即 "{{" 或 "}}"。To specify a single literal brace character in format, specify two leading or trailing brace characters; that is, "{{" or "}}".

此方法不会在指定的字符串中搜索各个换行符(十六进制0x000a)并将其替换TextWriter.NewLine为。This method does not search the specified string for individual newline characters (hexadecimal 0x000a) and replace them with TextWriter.NewLine.

如果在格式字符串中未引用指定的对象,则会将其忽略。If a specified object is not referenced in the format string, it is ignored.

有关常见 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 text stream, using the same semantics as the Format(String, Object) method.

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

参数

format
String

复合格式字符串。A composite format string.

arg0
Object

要格式化和写入的对象。The object to format and write.

异常

formatnullformat is null.

TextWriter 是关闭的。The TextWriter is closed.

出现 I/O 错误。An I/O error occurs.

format 不是有效的复合格式字符串。format is not a valid composite format string.

-or- 格式项的索引小于 0 (0)、或大于或等于要设置格式的对象数 (用于该方法重载,为一)。The index of a format item is less than 0 (zero), or greater than or equal to the number of objects to be formatted (which, for this method overload, is one).

注解

此方法使用复合格式设置将对象的值转换为其字符串表示形式,并将该表示形式嵌入字符串。This method uses composite formatting to convert the value of an object to its string representation and to embed that representation in a string. .NET 提供广泛的格式支持,以下格式设置主题对此进行了更详细的介绍:.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

format参数包含与零个或多个索引占位符(称为格式项)混合的零个或多个文本的运行,这些占位符与此方法的参数列表中的对象相对应。The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to an object in the parameter list of this method. 格式设置过程将每个格式项替换为相应对象的值的字符串表示形式。The formatting process replaces each format item with the string representation of the value of the corresponding object.

格式项的语法如下所示:The syntax of a format item is as follows:

{index[,length] [:格式字符串]}{index[,length][:formatString]}

括号中的元素是可选的。Elements in square brackets are optional. 下表对每个元素进行了描述。The following table describes each element. 有关复合格式设置功能的详细信息(包括格式项的语法),请参阅复合格式设置For more information about the composite formatting feature, including the syntax of a format item, see Composite Formatting.

元素Element 说明Description
索引index 要设置格式的对象的参数列表中的从零开始的位置。The zero-based position in the parameter list of the object to be formatted. 如果由index指定的对象为null,则会将格式String.Empty项替换为。If the object specified by index is null, the format item is replaced by String.Empty. 因为此重载在其参数列表中只有一个对象,所以index的值必须始终为0。Because this overload has only a single object in its parameter list, the value of index must always be 0. 如果索引位置中没有参数, FormatException则会引发。If there is no parameter in the index position, a FormatException is thrown.
length,length 参数的字符串表示形式中的最小字符数。The minimum number of characters in the string representation of the parameter. 如果为正,则参数为右对齐; 否则为。如果为负,则为左对齐。If positive, the parameter is right-aligned; if negative, it is left-aligned.
:formatString:formatString 要设置格式的对象支持的标准或自定义格式字符串。A standard or custom format string that is supported by the object to be formatted. 格式字符串的可能值与对象的ToString(string format)方法所支持的值相同。Possible values for formatString are the same as the values supported by the object's ToString(string format) method. 如果未指定 "格式字符串" 并且要设置其格式的IFormattable对象实现null接口, format则将作为用作IFormattable.ToString格式字符串的参数的值传递。If formatString is not specified and the object to be formatted implements the IFormattable interface, null is passed as the value of the format parameter that is used as the IFormattable.ToString format string.

需要 "{" 和 "}" 等前导和尾随大括号。The leading and trailing brace characters, "{" and "}", are required. 若要在中format指定单个文本大括号字符,请指定两个前导或尾随大括号字符,即 "{{" 或 "}}"。To specify a single literal brace character in format, specify two leading or trailing brace characters; that is, "{{" or "}}".

. 此方法不会在指定的字符串中搜索各个换行符(十六进制0x000a)并将其替换TextWriter.NewLine为。This method does not search the specified string for individual newline characters (hexadecimal 0x000a) and replace them with TextWriter.NewLine.

如果在格式字符串中未引用指定的对象,则会将其忽略。If a specified object is not referenced in the format string, it is ignored.

有关常见 i/o 任务的列表, 请参阅常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

另请参阅

Write(UInt64)

重要

此 API 不符合 CLS。

将 8 字节无符号整数的文本表示形式写入文本流。Writes the text representation of an 8-byte unsigned integer to the text stream.

public:
 virtual void Write(System::UInt64 value);
[System.CLSCompliant(false)]
public virtual void Write (ulong value);
abstract member Write : uint64 -> unit
override this.Write : uint64 -> unit
Public Overridable Sub Write (value As ULong)

参数

value
UInt64

要写入的 8 字节无符号整数。The 8-byte unsigned integer to write.

属性

异常

TextWriter 是关闭的。The TextWriter is closed.

出现 I/O 错误。An I/O error occurs.

注解

通过调用UInt64.ToString方法来生成指定值的文本表示形式。The text representation of the specified value is produced by calling the UInt64.ToString method. 如果TextWriter.FormatProvidernull是,则属性指定区域性特定的格式设置。The TextWriter.FormatProvider property, if not null, specifies the culture-specific formatting.

有关常见 i/o 任务的列表, 请参阅常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

另请参阅

Write(UInt32)

重要

此 API 不符合 CLS。

将 4 字节无符号整数的文本表示形式写入文本流。Writes the text representation of a 4-byte unsigned integer to the text stream.

public:
 virtual void Write(System::UInt32 value);
[System.CLSCompliant(false)]
public virtual void Write (uint value);
abstract member Write : uint32 -> unit
override this.Write : uint32 -> unit
Public Overridable Sub Write (value As UInteger)

参数

value
UInt32

要写入的 4 字节无符号整数。The 4-byte unsigned integer to write.

属性

异常

TextWriter 是关闭的。The TextWriter is closed.

出现 I/O 错误。An I/O error occurs.

注解

通过调用UInt32.ToString方法来生成指定值的文本表示形式。The text representation of the specified value is produced by calling the UInt32.ToString method. 如果TextWriter.FormatProvidernull是,则属性指定区域性特定的格式设置。The TextWriter.FormatProvider property, if not null, specifies the culture-specific formatting.

有关常见 i/o 任务的列表, 请参阅常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

另请参阅

Write(StringBuilder)

将字符串生成器写入文本流。Writes a string builder to the text stream.

public:
 virtual void Write(System::Text::StringBuilder ^ value);
public virtual void Write (System.Text.StringBuilder value);
abstract member Write : System.Text.StringBuilder -> unit
override this.Write : System.Text.StringBuilder -> unit
Public Overridable Sub Write (value As StringBuilder)

参数

value
StringBuilder

要作为字符串生成器写入文本流的字符串。The string, as a string builder, to write to the text stream.

注解

此方法等效于Write(stringBuilder.ToString()),但它StringBuilder.GetChunks使用方法来避免创建中间字符串。This method is equivalent to Write(stringBuilder.ToString()), but it uses the StringBuilder.GetChunks method to avoid creating the intermediate string.

Write(String)

将字符串写入文本流。Writes a string to the text stream.

public:
 virtual void Write(System::String ^ value);
public virtual void Write (string value);
abstract member Write : string -> unit
override this.Write : string -> unit
Public Overridable Sub Write (value As String)

参数

value
String

要写入的字符串。The string to write.

异常

TextWriter 是关闭的。The TextWriter is closed.

出现 I/O 错误。An I/O error occurs.

注解

此重载等效于Write(Char[])重载。This overload is equivalent to the Write(Char[]) overload.

此方法不会在指定的字符串中搜索各个换行符(十六进制0x000a)并将其替换TextWriter.NewLine为。This method does not search the specified string for individual newline characters (hexadecimal 0x000a) and replace them with TextWriter.NewLine.

如果 valuenull,则不会将任何内容写入文本流。If value is null, nothing is written to the text stream.

有关常见 i/o 任务的列表, 请参阅常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

另请参阅

Write(Single)

将 4 字节浮点值的文本表示形式写入文本流。Writes the text representation of a 4-byte floating-point value to the text stream.

public:
 virtual void Write(float value);
public virtual void Write (float value);
abstract member Write : single -> unit
override this.Write : single -> unit
Public Overridable Sub Write (value As Single)

参数

value
Single

要写入的 4 字节浮点值。The 4-byte floating-point value to write.

异常

TextWriter 是关闭的。The TextWriter is closed.

出现 I/O 错误。An I/O error occurs.

注解

通过调用Single.ToString方法来生成指定值的文本表示形式。The text representation of the specified value is produced by calling the Single.ToString method. 如果TextWriter.FormatProvidernull是,则属性指定区域性特定的格式设置。The TextWriter.FormatProvider property, if not null, specifies the culture-specific formatting.

有关常见 i/o 任务的列表, 请参阅常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

另请参阅

Write(String, Object, Object, Object)

使用与 Format(String, Object, Object, Object) 方法相同的语义将格式化字符串写入文本流。Writes a formatted string to the text stream, using the same semantics as the Format(String, Object, Object, Object) method.

public:
 virtual void Write(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1, System::Object ^ arg2);
public virtual void Write (string format, object arg0, object arg1, object arg2);
abstract member Write : string * obj * obj * obj -> unit
override this.Write : string * obj * obj * obj -> unit
Public Overridable 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.

异常

formatnullformat is null.

TextWriter 是关闭的。The TextWriter is closed.

出现 I/O 错误。An I/O error occurs.

format 不是有效的复合格式字符串。format is not a valid composite format string.

- 或 --or- 格式项的索引小于 0 (0)、或大于或等于要设置格式的对象数 (用于该方法重载,为三)。The index of a format item is less than 0 (zero), or greater than or equal to the number of objects to be formatted (which, for this method overload, is three).

注解

此方法使用复合格式设置将对象的值转换为其字符串表示形式,并将该表示形式嵌入字符串。This method uses composite formatting to convert the value of an object to its string representation and to embed that representation in a string. .NET 提供广泛的格式支持,以下格式设置主题对此进行了更详细的介绍:.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

format参数包含与零个或多个索引占位符(称为格式项)混合的零个或多个文本的运行,这些占位符与此方法的参数列表中的对象相对应。The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to an object in the parameter list of this method. 格式设置过程将每个格式项替换为相应对象的值的字符串表示形式。The formatting process replaces each format item with the string representation of the value of the corresponding object.

格式项的语法如下所示:The syntax of a format item is as follows:

{index[,length] [:格式字符串]}{index[,length][:formatString]}

括号中的元素是可选的。Elements in square brackets are optional. 下表对每个元素进行了描述。The following table describes each element. 有关复合格式设置功能的详细信息(包括格式项的语法),请参阅复合格式设置For more information about the composite formatting feature, including the syntax of a format item, see Composite Formatting.

元素Element 说明Description
索引index 要设置格式的对象的参数列表中的从零开始的位置。The zero-based position in the parameter list of the object to be formatted. 如果由index指定的对象为null,则会将格式String.Empty项替换为。If the object specified by index is null, the format item is replaced by String.Empty. 因为此重载在其参数列表中有三个对象,所以index的值必须始终是0、1或2。Because this overload has three objects in its parameter list, the value of index must always be 0, 1, or 2. 如果索引位置中没有参数, FormatException则会引发。If there is no parameter in the index position, a FormatException is thrown.
length,length 参数的字符串表示形式中的最小字符数。The minimum number of characters in the string representation of the parameter. 如果为正,则参数为右对齐; 否则为。如果为负,则为左对齐。If positive, the parameter is right-aligned; if negative, it is left-aligned.
:formatString:formatString 要设置格式的对象支持的标准或自定义格式字符串。A standard or custom format string that is supported by the object to be formatted. 格式字符串的可能值与对象的ToString(string format)方法所支持的值相同。Possible values for formatString are the same as the values supported by the object's ToString(string format) method. 如果未指定 "格式字符串" 并且要设置其格式的IFormattable对象实现null接口, format则将作为用作IFormattable.ToString格式字符串的参数的值传递。If formatString is not specified and the object to be formatted implements the IFormattable interface, null is passed as the value of the format parameter that is used as the IFormattable.ToString format string.

需要 "{" 和 "}" 等前导和尾随大括号。The leading and trailing brace characters, "{" and "}", are required. 若要在中format指定单个文本大括号字符,请指定两个前导或尾随大括号字符,即 "{{" 或 "}}"。To specify a single literal brace character in format, specify two leading or trailing brace characters; that is, "{{" or "}}".

此方法不会在指定的字符串中搜索各个换行符(十六进制0x000a)并将其替换TextWriter.NewLine为。This method does not search the specified string for individual newline characters (hexadecimal 0x000a) and replace them with TextWriter.NewLine.

如果在格式字符串中未引用指定的对象,则会将其忽略。If a specified object is not referenced in the format string, it is ignored.

有关常见 i/o 任务的列表, 请参阅常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

另请参阅

Write(Object)

通过在对象上调用 ToString 方法将此对象的文本表示形式写入文本流。Writes the text representation of an object to the text stream by calling the ToString method on that object.

public:
 virtual void Write(System::Object ^ value);
public virtual void Write (object value);
abstract member Write : obj -> unit
override this.Write : obj -> unit
Public Overridable Sub Write (value As Object)

参数

value
Object

要写入的对象。The object to write.

异常

TextWriter 是关闭的。The TextWriter is closed.

出现 I/O 错误。An I/O error occurs.

注解

此重载等效于Write(String)重载。This overload is equivalent to the Write(String) overload.

如果指定的对象是null,则不执行任何操作,也不会引发异常。If the specified object is null, no action is taken and no exception is thrown. 否则,将调用对象ToString的方法来生成字符串表示形式,然后将生成的字符串写入输出流。Otherwise, the object's ToString method is called to produce the string representation, and the resulting string is then written to the output stream. 如果FormatProvidernull是,则属性指定区域性特定的格式设置。The FormatProvider property, if not null, specifies the culture-specific formatting.

有关常见 i/o 任务的列表, 请参阅常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

另请参阅

Write(Int64)

将 8 字节带符号整数的文本表示形式写入文本流。Writes the text representation of an 8-byte signed integer to the text stream.

public:
 virtual void Write(long value);
public virtual void Write (long value);
abstract member Write : int64 -> unit
override this.Write : int64 -> unit
Public Overridable Sub Write (value As Long)

参数

value
Int64

要写入的 8 字节有符号整数。The 8-byte signed integer to write.

异常

TextWriter 是关闭的。The TextWriter is closed.

出现 I/O 错误。An I/O error occurs.

注解

通过调用Int64.ToString方法来生成指定值的文本表示形式。The text representation of the specified value is produced by calling the Int64.ToString method. 如果TextWriter.FormatProvidernull是,则属性指定区域性特定的格式设置。The TextWriter.FormatProvider property, if not null, specifies the culture-specific formatting.

有关常见 i/o 任务的列表, 请参阅常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

如何:向文件写入文本How to: Write Text to a File

另请参阅

Write(Int32)

将 4 字节带符号整数的文本表示形式写入文本流。Writes the text representation of a 4-byte signed integer to the text stream.

public:
 virtual void Write(int value);
public virtual void Write (int value);
abstract member Write : int -> unit
override this.Write : int -> unit
Public Overridable Sub Write (value As Integer)

参数

value
Int32

要写入的 4 字节有符号整数。The 4-byte signed integer to write.

异常

TextWriter 是关闭的。The TextWriter is closed.

出现 I/O 错误。An I/O error occurs.

注解

通过调用Int32.ToString方法来生成指定值的文本表示形式。The text representation of the specified value is produced by calling the Int32.ToString method. 如果TextWriter.FormatProvidernull是,则属性指定区域性特定的格式设置。The TextWriter.FormatProvider property, if not null, specifies the culture-specific formatting.

有关常见 i/o 任务的列表, 请参阅常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

另请参阅

Write(Double)

将 8 字节浮点值的文本表示形式写入文本流。Writes the text representation of an 8-byte floating-point value to the text stream.

public:
 virtual void Write(double value);
public virtual void Write (double value);
abstract member Write : double -> unit
override this.Write : double -> unit
Public Overridable Sub Write (value As Double)

参数

value
Double

要写入的 8 字节浮点值。The 8-byte floating-point value to write.

异常

TextWriter 是关闭的。The TextWriter is closed.

出现 I/O 错误。An I/O error occurs.

注解

通过调用Double.ToString方法来生成指定值的文本表示形式。The text representation of the specified value is produced by calling the Double.ToString method. 如果TextWriter.FormatProvidernull是,则属性指定区域性特定的格式设置。The TextWriter.FormatProvider property, if not null, specifies the culture-specific formatting.

有关常见 i/o 任务的列表, 请参阅常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

另请参阅

Write(Decimal)

将小数值的文本表示形式写入文本流。Writes the text representation of a decimal value to the text stream.

public:
 virtual void Write(System::Decimal value);
public virtual void Write (decimal value);
abstract member Write : decimal -> unit
override this.Write : decimal -> unit
Public Overridable Sub Write (value As Decimal)

参数

value
Decimal

要写入的十进制值。The decimal value to write.

异常

TextWriter 是关闭的。The TextWriter is closed.

出现 I/O 错误。An I/O error occurs.

注解

如果FormatProvidernull是,则属性指定区域性特定的格式设置。The FormatProvider property, if not null, specifies the culture-specific formatting.

另请参阅

Write(Char[])

将字符数组写入文本流。Writes a character array to the text stream.

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

参数

buffer
Char[]

要写入文本流中的字符数组。The character array to write to the text stream.

异常

TextWriter 是关闭的。The TextWriter is closed.

出现 I/O 错误。An I/O error occurs.

注解

此方法不会在指定的字符串中搜索各个换行符(十六进制0x000a)并将其替换NewLine为。This method does not search the specified string for individual newline characters (hexadecimal 0x000a) and replace them with NewLine.

此默认方法调用Write(Char[], Int32, Int32)方法并传递整个字符数组。This default method calls the Write(Char[], Int32, Int32) method and passes the entire character array. 如果字符数组为null,则不写入任何内容。If the character array is null, nothing is written.

有关常见 i/o 任务的列表, 请参阅常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

另请参阅

Write(Char)

将字符写入文本流。Writes a character to the text stream.

public:
 virtual void Write(char value);
public virtual void Write (char value);
abstract member Write : char -> unit
override this.Write : char -> unit
Public Overridable Sub Write (value As Char)

参数

value
Char

要写入文本流中的字符。The character to write to the text stream.

异常

TextWriter 是关闭的。The TextWriter is closed.

出现 I/O 错误。An I/O error occurs.

注解

此默认方法不执行任何操作,但派生的类可以重写方法以提供相应的功能。This default method does nothing, but derived classes can override the method to provide the appropriate functionality.

有关常见 i/o 任务的列表, 请参阅常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

另请参阅

Write(Boolean)

Boolean 值的文本表示形式写入文本流。Writes the text representation of a Boolean value to the text stream.

public:
 virtual void Write(bool value);
public virtual void Write (bool value);
abstract member Write : bool -> unit
override this.Write : bool -> unit
Public Overridable Sub Write (value As Boolean)

参数

value
Boolean

要写入的 Boolean 值。The Boolean value to write.

异常

TextWriter 是关闭的。The TextWriter is closed.

出现 I/O 错误。An I/O error occurs.

注解

通过调用Boolean.ToString方法来生成指定值的文本表示形式。The text representation of the specified value is produced by calling the Boolean.ToString method.

此方法输出Boolean.TrueStringBoolean.FalseStringThis method outputs either Boolean.TrueString or Boolean.FalseString.

有关常见 i/o 任务的列表, 请参阅常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

另请参阅

Write(ReadOnlySpan<Char>)

将字符范围写入文本流。Writes a character span to the text stream.

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

参数

buffer
ReadOnlySpan<Char>

要写入的字符范围。The character span to write.

注解

通过调用ReadOnlySpan生成指定值的文本表示形式。ToString方法。The text representation of the specified value is produced by calling the ReadOnlySpan.ToString method.

有关常见 i/o 任务的列表, 请参阅常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

适用于