StringWriter 类

定义

实现用于将信息写入字符串的 TextWriter。 信息存储在基础 StringBuilder 中。

public ref class StringWriter : System::IO::TextWriter
public class StringWriter : System.IO.TextWriter
[System.Serializable]
public class StringWriter : System.IO.TextWriter
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class StringWriter : System.IO.TextWriter
type StringWriter = class
    inherit TextWriter
[<System.Serializable>]
type StringWriter = class
    inherit TextWriter
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StringWriter = class
    inherit TextWriter
Public Class StringWriter
Inherits TextWriter
继承
StringWriter
继承
属性

示例

下面的代码示例演示如何从一组双间距句子创建连续段落,然后将该段落转换回原始文本。

using namespace System;
using namespace System::IO;
int main()
{
   String^ textReaderText = "TextReader is the abstract base "
   "class of StreamReader and StringReader, which read "
   "characters from streams and strings, respectively.\n\n"
   "Create an instance of TextReader to open a text file "
   "for reading a specified range of characters, or to "
   "create a reader based on an existing stream.\n\n"
   "You can also use an instance of TextReader to read "
   "text from a custom backing store using the same "
   "APIs you would use for a string or a stream.\n\n";
   Console::WriteLine(  "Original text:\n\n{0}", textReaderText );

   // From textReaderText, create a continuous paragraph 
   // with two spaces between each sentence.
      String^ aLine;
   String^ aParagraph;
   StringReader^ strReader = gcnew StringReader( textReaderText );
   while ( true )
   {
      aLine = strReader->ReadLine();
      if ( aLine != nullptr )
      {
         aParagraph = String::Concat( aParagraph, aLine,  " " );
      }
      else
      {
         aParagraph = String::Concat( aParagraph,  "\n" );
         break;
      }
   }

   Console::WriteLine(  "Modified text:\n\n{0}", aParagraph );
   
   // Re-create textReaderText from aParagraph.
   int intCharacter;
   Char convertedCharacter;
   StringWriter^ strWriter = gcnew StringWriter;
   strReader = gcnew StringReader( aParagraph );
   while ( true )
   {
      intCharacter = strReader->Read();
      
      // Check for the end of the string 
      // before converting to a character.
      if ( intCharacter == -1 )
            break;

      
      convertedCharacter = Convert::ToChar( intCharacter );
      if ( convertedCharacter == '.' )
      {
         strWriter->Write(  ".\n\n" );
         
         // Bypass the spaces between sentences.
         strReader->Read();
         strReader->Read();
      }
      else
      {
         strWriter->Write( convertedCharacter );
      }
   }

   Console::WriteLine(  "\nOriginal text:\n\n{0}", strWriter->ToString() );
}
using System;
using System.IO;

class StringRW
{
    static void Main()
    {
        string textReaderText = "TextReader is the abstract base " +
            "class of StreamReader and StringReader, which read " +
            "characters from streams and strings, respectively.\n\n" +

            "Create an instance of TextReader to open a text file " +
            "for reading a specified range of characters, or to " +
            "create a reader based on an existing stream.\n\n" +

            "You can also use an instance of TextReader to read " +
            "text from a custom backing store using the same " +
            "APIs you would use for a string or a stream.\n\n";

        Console.WriteLine("Original text:\n\n{0}", textReaderText);

        // From textReaderText, create a continuous paragraph
        // with two spaces between each sentence.
        string aLine, aParagraph = null;
        StringReader strReader = new StringReader(textReaderText);
        while(true)
        {
            aLine = strReader.ReadLine();
            if(aLine != null)
            {
                aParagraph = aParagraph + aLine + " ";
            }
            else
            {
                aParagraph = aParagraph + "\n";
                break;
            }
        }
        Console.WriteLine("Modified text:\n\n{0}", aParagraph);

        // Re-create textReaderText from aParagraph.
        int intCharacter;
        char convertedCharacter;
        StringWriter strWriter = new StringWriter();
        strReader = new StringReader(aParagraph);
        while(true)
        {
            intCharacter = strReader.Read();

            // Check for the end of the string
            // before converting to a character.
            if(intCharacter == -1) break;

            convertedCharacter = (char)intCharacter;
            if(convertedCharacter == '.')
            {
                strWriter.Write(".\n\n");

                // Bypass the spaces between sentences.
                strReader.Read();
                strReader.Read();
            }
            else
            {
                strWriter.Write(convertedCharacter);
            }
        }
        Console.WriteLine("\nOriginal text:\n\n{0}",
            strWriter.ToString());
    }
}
Option Explicit
Option Strict

Imports System.IO

Public Class StrReader

    Shared Sub Main()
    
        Dim textReaderText As String = "TextReader is the " & _
            "abstract base class of StreamReader and " & _
            "StringReader, which read characters from streams " & _
            "and strings, respectively." & _
            vbCrLf & vbCrLf & _
            "Create an instance of TextReader to open a text " & _
            "file for reading a specified range of characters, " & _
            "or to create a reader based on an existing stream." & _
            vbCrLf & vbCrLf & _
            "You can also use an instance of TextReader to read " & _
            "text from a custom backing store using the same " & _
            "APIs you would use for a string or a stream." & _
            vbCrLf & vbCrLf

        Console.WriteLine("Original text:" & vbCrLf & vbCrLf & _
            textReaderText)

        ' From textReaderText, create a continuous paragraph 
        ' with two spaces between each sentence.
        Dim aLine, aParagraph As String
        Dim strReader As New StringReader(textReaderText)
        While True
            aLine = strReader.ReadLine()
            If aLine Is Nothing Then
                aParagraph = aParagraph & vbCrLf
                Exit While
            Else
                aParagraph = aParagraph & aLine & " "
            End If
        End While
        Console.WriteLine("Modified text:" & vbCrLf & vbCrLf & _ 
            aParagraph)
    
        ' Re-create textReaderText from aParagraph.
        Dim intCharacter As Integer 
        Dim convertedCharacter As Char 
        Dim strWriter As New StringWriter()
        strReader = New StringReader(aParagraph)
        While True
            intCharacter = strReader.Read()

            ' Check for the end of the string 
            ' before converting to a character.
            If intCharacter = -1 Then
                Exit While
            End If

            convertedCharacter = Convert.ToChar(intCharacter)
            If convertedCharacter = "."C Then
                strWriter.Write("." & vbCrLf & vbCrLf)

                ' Bypass the spaces between sentences.
                strReader.Read()
                strReader.Read()
            Else
                strWriter.Write(convertedCharacter)
            End If
        End While
        Console.WriteLine(vbCrLf & "Original text:" & vbCrLf & _ 
            vbCrLf & strWriter.ToString())

    End Sub
End Class

注解

StringWriter 使你能够以同步或异步方式写入字符串。 可以使用 或 WriteAsync(Char) 方法一次Write(Char)写入字符,使用 Write(String)WriteAsync(String) 方法一次写入字符串。 此外,可以使用 方法之一异步编写字符、字符数组或后跟行终止符的 WriteLineAsync 字符串。

注意

此类型可实现 IDisposable 接口,但实际上没有任何要释放的资源。 这意味着无需通过直接调用 Dispose() 或使用语言构造(C# 中的 using 或 Visual Basic 中的 Using)对其进行处理。

下表列出了其他典型或相关 I/O 任务的示例。

若要执行此操作... 请参见本主题中的示例...
创建文本文件。 如何:将文本写入文件
写入文本文件。 如何:将文本写入文件
从文本文件读取。 如何:从文件中读取文本
将文本追加到文件。 如何:打开并追加到日志文件

File.AppendText

FileInfo.AppendText
获取文件的大小。 FileInfo.Length
获取文件的属性。 File.GetAttributes
设置文件的属性。 File.SetAttributes
确定文件是否存在。 File.Exists
从二进制文件读取。 如何:对新建的数据文件进行读取和写入
写入二进制文件。 如何:对新建的数据文件进行读取和写入

构造函数

StringWriter()

初始化 StringWriter 类的新实例。

StringWriter(IFormatProvider)

使用指定的格式控件初始化 StringWriter 类的新实例。

StringWriter(StringBuilder)

初始化写入到指定的 StringBuilder 中的 StringWriter 类的新实例。

StringWriter(StringBuilder, IFormatProvider)

初始化 StringWriter 类的新实例,该实例写入指定 StringBuilder ,且具有指定的格式提供程序。

字段

CoreNewLine

存储用于此 TextWriter 的换行符。

(继承自 TextWriter)

属性

Encoding

获取在其中写入输出的 Encoding

FormatProvider

获取控制格式设置的对象。

(继承自 TextWriter)
NewLine

获取或设置由当前 TextWriter 使用的行结束符字符串。

(继承自 TextWriter)

方法

Close()

关闭当前的 StringWriter 和基础流。

Close()

关闭当前编写器并释放任何与该编写器关联的系统资源。

(继承自 TextWriter)
CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。

(继承自 MarshalByRefObject)
Dispose()

释放由 TextWriter 对象使用的所有资源。

(继承自 TextWriter)
Dispose(Boolean)

释放由 StringWriter 占用的非托管资源,还可以另外再释放托管资源。

DisposeAsync()

异步释放由 TextWriter 对象使用的所有资源。

(继承自 TextWriter)
Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
Flush()

清理当前编写器的所有缓冲区,使所有缓冲数据写入基础设备。

(继承自 TextWriter)
FlushAsync()

异步清理当前编写器的所有缓冲区,使所有缓冲数据写入基础设备。

FlushAsync()

异步清理当前编写器的所有缓冲区,使所有缓冲数据写入基础设备。

(继承自 TextWriter)
FlushAsync(CancellationToken)

异步清理当前编写器的所有缓冲区,使所有缓冲数据写入基础设备。

(继承自 TextWriter)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetLifetimeService()
已过时.

检索控制此实例的生存期策略的当前生存期服务对象。

(继承自 MarshalByRefObject)
GetStringBuilder()

返回基础 StringBuilder

GetType()

获取当前实例的 Type

(继承自 Object)
InitializeLifetimeService()
已过时.

获取生存期服务对象来控制此实例的生存期策略。

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。

(继承自 MarshalByRefObject)
ToString()

返回包含迄今为止写入到当前 StringWriter 中的字符的字符串。

Write(Boolean)

Boolean 值的文本表示形式写入文本流。

(继承自 TextWriter)
Write(Char)

将字符写入字符串。

Write(Char[])

将字符数组写入文本流。

(继承自 TextWriter)
Write(Char[], Int32, Int32)

将字符的子数组写入字符串。

Write(Decimal)

将小数值的文本表示形式写入文本流。

(继承自 TextWriter)
Write(Double)

将 8 字节浮点值的文本表示形式写入文本流。

(继承自 TextWriter)
Write(Int32)

将 4 字节带符号整数的文本表示形式写入文本流。

(继承自 TextWriter)
Write(Int64)

将 8 字节带符号整数的文本表示形式写入文本流。

(继承自 TextWriter)
Write(Object)

通过在对象上调用 ToString 方法将此对象的文本表示形式写入文本流。

(继承自 TextWriter)
Write(ReadOnlySpan<Char>)

将字符范围的字符串表示形式写入当前字符串。

Write(ReadOnlySpan<Char>)

将字符范围写入文本流。

(继承自 TextWriter)
Write(Single)

将 4 字节浮点值的文本表示形式写入文本流。

(继承自 TextWriter)
Write(String)

将字符串写入当前字符串。

Write(String, Object)

使用与 Format(String, Object) 方法相同的语义将格式化字符串写入文本流。

(继承自 TextWriter)
Write(String, Object, Object)

使用与 Format(String, Object, Object) 方法相同的语义将格式化字符串写入文本流。

(继承自 TextWriter)
Write(String, Object, Object, Object)

使用与 Format(String, Object, Object, Object) 方法相同的语义将格式化字符串写入文本流。

(继承自 TextWriter)
Write(String, Object[])

使用与 Format(String, Object[]) 方法相同的语义将格式化字符串写入文本流。

(继承自 TextWriter)
Write(StringBuilder)

将字符串生成器的字符串表示形式写入当前字符串。

Write(StringBuilder)

将字符串生成器写入文本流。

(继承自 TextWriter)
Write(UInt32)

将 4 字节无符号整数的文本表示形式写入文本流。

(继承自 TextWriter)
Write(UInt64)

将 8 字节无符号整数的文本表示形式写入文本流。

(继承自 TextWriter)
WriteAsync(Char)

将字符异步写入该字符串。

WriteAsync(Char)

将字符异步写入文本流。

(继承自 TextWriter)
WriteAsync(Char[])

将字符数组异步写入文本流。

(继承自 TextWriter)
WriteAsync(Char[], Int32, Int32)

将字符的子数组异步写入字符串。

WriteAsync(Char[], Int32, Int32)

以异步形式将字符的子数组写入文本流。

(继承自 TextWriter)
WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

将字符的内存区域异步写入字符串。

WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

以异步形式将字符内存区域写入文本流。

(继承自 TextWriter)
WriteAsync(String)

将字符串异步写入当前字符串。

WriteAsync(String)

将字符串异步写入文本流。

(继承自 TextWriter)
WriteAsync(StringBuilder, CancellationToken)

将字符串生成器的文本表示形式异步写入字符串。

WriteAsync(StringBuilder, CancellationToken)

以异步形式将字符串生成器写入文本流。

(继承自 TextWriter)
WriteLine()

将行终止符写入文本流。

(继承自 TextWriter)
WriteLine(Boolean)

Boolean 值的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLine(Char)

将字符写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLine(Char[])

将字符数组写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLine(Char[], Int32, Int32)

将字符子数组写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLine(Decimal)

将小数值的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLine(Double)

将 8 字节浮点值的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLine(Int32)

将 4 字节带符号整数的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLine(Int64)

将 8 字节带符号整数的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLine(Object)

通过在对象上调用 ToString 方法将此对象的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLine(ReadOnlySpan<Char>)

将字符范围的文本表示形式写入字符串,后跟行终止符。

WriteLine(ReadOnlySpan<Char>)

将字符范围的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLine(Single)

将 4 字节浮点值的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLine(String)

将字符串写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLine(String, Object)

使用与 Format(String, Object) 方法相同的语义将格式化字符串和新行写入文本流。

(继承自 TextWriter)
WriteLine(String, Object, Object)

使用与 Format(String, Object, Object) 方法相同的语义将格式化字符串和新行写入文本流。

(继承自 TextWriter)
WriteLine(String, Object, Object, Object)

使用与 Format(String, Object) 相同的语义将格式化字符串和新行写入文本流。

(继承自 TextWriter)
WriteLine(String, Object[])

使用与 Format(String, Object) 相同的语义将格式化字符串和新行写入文本流。

(继承自 TextWriter)
WriteLine(StringBuilder)

将字符串生成器的文本表示形式写入字符串,后跟行终止符。

WriteLine(StringBuilder)

将字符串生成器的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLine(UInt32)

将 4 字节无符号整数的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLine(UInt64)

将 8 字节无符号整数的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLineAsync()

以异步形式将行终止符写入文本流。

(继承自 TextWriter)
WriteLineAsync(Char)

将字符异步写入字符串,后跟行终止符。

WriteLineAsync(Char)

以异步形式将字符写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLineAsync(Char[])

以异步形式将字符数组写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLineAsync(Char[], Int32, Int32)

将字符的子数组异步写入字符串,后跟行终止符。

WriteLineAsync(Char[], Int32, Int32)

以异步形式将字符子数组写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

将字符内存区域的字符串表示形式异步写入当前字符串,后跟行终止符。

WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

以异步形式将字符内存区域的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLineAsync(String)

将字符串异步写入当前字符串,后跟行终止符。

WriteLineAsync(String)

以异步形式将字符串写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLineAsync(StringBuilder, CancellationToken)

将字符串生成器的字符串表示形式异步写入当前字符串,后跟行终止符。

WriteLineAsync(StringBuilder, CancellationToken)

以异步形式将字符串生成器的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)

显式接口实现

IDisposable.Dispose()

有关此成员的说明,请参见 Dispose()

(继承自 TextWriter)

扩展方法

ConfigureAwait(IAsyncDisposable, Boolean)

配置如何执行从异步可处置项返回的任务的等待。

适用于

另请参阅