StringWriter 构造函数
定义
初始化 StringWriter 类的新实例。Initializes a new instance of the StringWriter class.
重载
| StringWriter() |
初始化 StringWriter 类的新实例。Initializes a new instance of the StringWriter class. |
| StringWriter(IFormatProvider) |
使用指定的格式控件初始化 StringWriter 类的新实例。Initializes a new instance of the StringWriter class with the specified format control. |
| StringWriter(StringBuilder) |
初始化写入到指定的 StringBuilder 中的 StringWriter 类的新实例。Initializes a new instance of the StringWriter class that writes to the specified StringBuilder. |
| StringWriter(StringBuilder, IFormatProvider) |
初始化 StringWriter 类的新实例,该实例写入指定 StringBuilder ,且具有指定的格式提供程序。Initializes a new instance of the StringWriter class that writes to the specified StringBuilder and has the specified format provider. |
StringWriter()
初始化 StringWriter 类的新实例。Initializes a new instance of the StringWriter class.
public:
StringWriter();
public StringWriter ();
Public Sub New ()
示例
下面的代码示例演示如何使用类构造字符串 StringWriter 。The following code example demonstrates how to construct a string using the StringWriter class.
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
StringWriter^ strWriter = gcnew StringWriter;
// Use the three overloads of the Write method that are
// overridden by the StringWriter class.
strWriter->Write( "file path characters are: " );
strWriter->Write( Path::InvalidPathChars, 0, Path::InvalidPathChars->Length );
strWriter->Write( Char::Parse( "." ) );
// Use the underlying StringBuilder for more complex
// manipulations of the string.
strWriter->GetStringBuilder()->Insert( 0, "Invalid " );
Console::WriteLine( "The following string is {0} encoded.\n{1}", strWriter->Encoding->EncodingName, strWriter->ToString() );
}
using System;
using System.IO;
using System.Text;
class StrWriter
{
static void Main()
{
StringWriter strWriter = new StringWriter();
// Use the three overloads of the Write method that are
// overridden by the StringWriter class.
strWriter.Write("file path characters are: ");
strWriter.Write(
Path.InvalidPathChars, 0, Path.InvalidPathChars.Length);
strWriter.Write('.');
// Use the underlying StringBuilder for more complex
// manipulations of the string.
strWriter.GetStringBuilder().Insert(0, "Invalid ");
Console.WriteLine("The following string is {0} encoded.\n{1}",
strWriter.Encoding.EncodingName, strWriter.ToString());
}
}
Imports System.IO
Imports System.Text
Public Class StrWriter
Shared Sub Main()
Dim strWriter As StringWriter = new StringWriter()
' Use the three overloads of the Write method that are
' overridden by the StringWriter class.
strWriter.Write("file path characters are: ")
strWriter.Write( _
Path.InvalidPathChars, 0, Path.InvalidPathChars.Length)
strWriter.Write("."C)
' Use the underlying StringBuilder for more complex
' manipulations of the string.
strWriter.GetStringBuilder().Insert(0, "Invalid ")
Console.WriteLine("The following string is {0} encoded." _
& vbCrLf & "{1}", _
strWriter.Encoding.EncodingName, strWriter.ToString())
End Sub
End Class
注解
StringBuilder将自动创建一个新的对象,并将其与类的新实例相关联 StringWriter 。A new StringBuilder object is automatically created and associated with the new instance of the StringWriter class. 由于没有为此构造函数指定格式控制,将使用初始化新的实例 CultureInfo.CurrentCulture 。Since a format control is not specified for this constructor, the new instance will be initialized with CultureInfo.CurrentCulture.
下表列出了其他典型或相关 i/o 任务的示例。The following table lists examples of other typical or related I/O tasks.
| 若要执行此操作...To do this... | 请参见本主题中的示例...See the example in this topic... |
|---|---|
| 创建文本文件。Create a text file. | 如何:将文本写入文件How to: Write Text to a File |
| 写入文本文件。Write to a text file. | 如何:将文本写入文件How to: Write Text to a File |
| 读取文本文件。Read from a text file. | 如何:从文件中读取文本How to: Read Text from a File |
| 向文件追加文本。Append text to a file. | 如何:打开并追加到日志文件How to: Open and Append to a Log File File.AppendText FileInfo.AppendText |
| 获取文件的大小。Get the size of a file. | FileInfo.Length |
| 获取文件的属性。Get the attributes of a file. | File.GetAttributes |
| 设置文件的属性。Set the attributes of a file. | File.SetAttributes |
| 确定文件是否存在。Determine if a file exists. | File.Exists |
| 从二进制文件中读取。Read from a binary file. | 如何:对新建的数据文件进行读取和写入How to: Read and Write to a Newly Created Data File |
| 写入二进制文件。Write to a binary file. | 如何:对新建的数据文件进行读取和写入How to: Read and Write to a Newly Created Data File |
适用于
StringWriter(IFormatProvider)
使用指定的格式控件初始化 StringWriter 类的新实例。Initializes a new instance of the StringWriter class with the specified format control.
public:
StringWriter(IFormatProvider ^ formatProvider);
public StringWriter (IFormatProvider formatProvider);
public StringWriter (IFormatProvider? formatProvider);
new System.IO.StringWriter : IFormatProvider -> System.IO.StringWriter
Public Sub New (formatProvider As IFormatProvider)
参数
- formatProvider
- IFormatProvider
控制格式设置的 IFormatProvider 对象。An IFormatProvider object that controls formatting.
示例
下面的代码示例演示如何构造特定区域性中的字符串。The following code example demonstrates how to construct a string in a specific culture.
using namespace System;
using namespace System::Globalization;
using namespace System::IO;
int main()
{
StringWriter^ strWriter = gcnew StringWriter( gcnew CultureInfo( "ar-DZ" ) );
strWriter->Write( DateTime::Now );
Console::WriteLine( "Current date and time using the invariant culture: {0}\n"
"Current date and time using the Algerian culture: {1}", DateTime::Now.ToString(), strWriter->ToString() );
}
using System;
using System.Globalization;
using System.IO;
class StrWriter
{
static void Main()
{
StringWriter strWriter =
new StringWriter(new CultureInfo("ar-DZ"));
strWriter.Write(DateTime.Now);
Console.WriteLine(
"Current date and time using the invariant culture: {0}\n" +
"Current date and time using the Algerian culture: {1}",
DateTime.Now.ToString(), strWriter.ToString());
}
}
Imports System.Globalization
Imports System.IO
Public Class StrWriter
Shared Sub Main()
Dim strWriter As New StringWriter(New CultureInfo("ar-DZ"))
strWriter.Write(DateTime.Now)
Console.WriteLine( _
"Current date and time using the invariant culture: {0}" _
& vbCrLf & _
"Current date and time using the Algerian culture: {1}", _
DateTime.Now.ToString(), strWriter.ToString())
End Sub
End Class
注解
StringBuilder将自动创建一个新的对象,并将其与类的新实例相关联 StringWriter 。A new StringBuilder object is automatically created and associated with the new instance of the StringWriter class.
下表列出了其他典型或相关 i/o 任务的示例。The following table lists examples of other typical or related I/O tasks.
| 若要执行此操作...To do this... | 请参见本主题中的示例...See the example in this topic... |
|---|---|
| 创建文本文件。Create a text file. | 如何:将文本写入文件How to: Write Text to a File |
| 写入文本文件。Write to a text file. | 如何:将文本写入文件How to: Write Text to a File |
| 读取文本文件。Read from a text file. | 如何:从文件中读取文本How to: Read Text from a File |
| 向文件追加文本。Append text to a file. | 如何:打开并追加到日志文件How to: Open and Append to a Log File File.AppendText FileInfo.AppendText |
| 获取文件的大小。Get the size of a file. | FileInfo.Length |
| 获取文件的属性。Get the attributes of a file. | File.GetAttributes |
| 设置文件的属性。Set the attributes of a file. | File.SetAttributes |
| 确定文件是否存在。Determine if a file exists. | File.Exists |
| 从二进制文件中读取。Read from a binary file. | 如何:对新建的数据文件进行读取和写入How to: Read and Write to a Newly Created Data File |
| 写入二进制文件。Write to a binary file. | 如何:对新建的数据文件进行读取和写入How to: Read and Write to a Newly Created Data File |
适用于
StringWriter(StringBuilder)
初始化写入到指定的 StringBuilder 中的 StringWriter 类的新实例。Initializes a new instance of the StringWriter class that writes to the specified StringBuilder.
public:
StringWriter(System::Text::StringBuilder ^ sb);
public StringWriter (System.Text.StringBuilder sb);
new System.IO.StringWriter : System.Text.StringBuilder -> System.IO.StringWriter
Public Sub New (sb As StringBuilder)
参数
要写入的 StringBuilder 对象。The StringBuilder object to write to.
例外
sb 为 null。sb is null.
示例
下面的代码示例演示如何使用 StringBuilder 类修改已关闭的中的基础字符串 StringWriter 。The following code example demonstrates using the StringBuilder class to modify the underlying string in a closed StringWriter.
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
StringBuilder^ strBuilder = gcnew StringBuilder( "file path characters are: " );
StringWriter^ strWriter = gcnew StringWriter( strBuilder );
strWriter->Write( Path::InvalidPathChars, 0, Path::InvalidPathChars->Length );
strWriter->Close();
// Since the StringWriter is closed, an exception will
// be thrown if the Write method is called. However,
// the StringBuilder can still manipulate the string.
strBuilder->Insert( 0, "Invalid " );
Console::WriteLine( strWriter->ToString() );
}
using System;
using System.IO;
using System.Text;
class StrWriter
{
static void Main()
{
StringBuilder strBuilder =
new StringBuilder("file path characters are: ");
StringWriter strWriter = new StringWriter(strBuilder);
strWriter.Write(
Path.InvalidPathChars, 0, Path.InvalidPathChars.Length);
strWriter.Close();
// Since the StringWriter is closed, an exception will
// be thrown if the Write method is called. However,
// the StringBuilder can still manipulate the string.
strBuilder.Insert(0, "Invalid ");
Console.WriteLine(strWriter.ToString());
}
}
Imports System.IO
Imports System.Text
Public Class StrWriter
Shared Sub Main()
Dim strBuilder As New StringBuilder( _
"file path characters are: ")
Dim strWriter As New StringWriter(strBuilder)
strWriter.Write( _
Path.InvalidPathChars, 0, Path.InvalidPathChars.Length)
strWriter.Close()
' Since the StringWriter is closed, an exception will
' be thrown if the Write method is called. However,
' the StringBuilder can still manipulate the string.
strBuilder.Insert(0, "Invalid ")
Console.WriteLine(strWriter.ToString())
End Sub
End Class
注解
由于没有为此构造函数指定格式控制,将使用初始化新的实例 CultureInfo.CurrentCulture 。Since a format control is not specified for this constructor, the new instance will be initialized with CultureInfo.CurrentCulture.
下表列出了其他典型或相关 i/o 任务的示例。The following table lists examples of other typical or related I/O tasks.
| 若要执行此操作...To do this... | 请参见本主题中的示例...See the example in this topic... |
|---|---|
| 创建文本文件。Create a text file. | 如何:将文本写入文件How to: Write Text to a File |
| 写入文本文件。Write to a text file. | 如何:将文本写入文件How to: Write Text to a File |
| 读取文本文件。Read from a text file. | 如何:从文件中读取文本How to: Read Text from a File |
| 向文件追加文本。Append text to a file. | 如何:打开并追加到日志文件How to: Open and Append to a Log File File.AppendText FileInfo.AppendText |
| 获取文件的大小。Get the size of a file. | FileInfo.Length |
| 获取文件的属性。Get the attributes of a file. | File.GetAttributes |
| 设置文件的属性。Set the attributes of a file. | File.SetAttributes |
| 确定文件是否存在。Determine if a file exists. | File.Exists |
| 从二进制文件中读取。Read from a binary file. | 如何:对新建的数据文件进行读取和写入How to: Read and Write to a Newly Created Data File |
| 写入二进制文件。Write to a binary file. | 如何:对新建的数据文件进行读取和写入How to: Read and Write to a Newly Created Data File |
适用于
StringWriter(StringBuilder, IFormatProvider)
初始化 StringWriter 类的新实例,该实例写入指定 StringBuilder ,且具有指定的格式提供程序。Initializes a new instance of the StringWriter class that writes to the specified StringBuilder and has the specified format provider.
public:
StringWriter(System::Text::StringBuilder ^ sb, IFormatProvider ^ formatProvider);
public StringWriter (System.Text.StringBuilder sb, IFormatProvider formatProvider);
public StringWriter (System.Text.StringBuilder sb, IFormatProvider? formatProvider);
new System.IO.StringWriter : System.Text.StringBuilder * IFormatProvider -> System.IO.StringWriter
Public Sub New (sb As StringBuilder, formatProvider As IFormatProvider)
参数
要写入的 StringBuilder 对象。The StringBuilder object to write to.
- formatProvider
- IFormatProvider
控制格式设置的 IFormatProvider 对象。An IFormatProvider object that controls formatting.
例外
sb 为 null。sb is null.
注解
下表列出了其他典型或相关 i/o 任务的示例。The following table lists examples of other typical or related I/O tasks.
| 若要执行此操作...To do this... | 请参见本主题中的示例...See the example in this topic... |
|---|---|
| 创建文本文件。Create a text file. | 如何:将文本写入文件How to: Write Text to a File |
| 写入文本文件。Write to a text file. | 如何:将文本写入文件How to: Write Text to a File |
| 读取文本文件。Read from a text file. | 如何:从文件中读取文本How to: Read Text from a File |
| 向文件追加文本。Append text to a file. | 如何:打开并追加到日志文件How to: Open and Append to a Log File File.AppendText FileInfo.AppendText |
| 获取文件的大小。Get the size of a file. | FileInfo.Length |
| 获取文件的属性。Get the attributes of a file. | File.GetAttributes |
| 设置文件的属性。Set the attributes of a file. | File.SetAttributes |
| 确定文件是否存在。Determine if a file exists. | File.Exists |
| 从二进制文件中读取。Read from a binary file. | 如何:对新建的数据文件进行读取和写入How to: Read and Write to a Newly Created Data File |
| 写入二进制文件。Write to a binary file. | 如何:对新建的数据文件进行读取和写入How to: Read and Write to a Newly Created Data File |