StringWriter クラス

文字列に情報を書き込む TextWriter を実装します。情報は、基になる StringBuilder に格納されます。

この型のすべてのメンバの一覧については、StringWriter メンバ を参照してください。

System.Object
   System.MarshalByRefObject
      System.IO.TextWriter
         System.IO.StringWriter

<Serializable>
Public Class StringWriter   Inherits TextWriter
[C#]
[Serializable]
public class StringWriter : TextWriter
[C++]
[Serializable]
public __gc class StringWriter : public TextWriter
[JScript]
public
   Serializable
class StringWriter extends TextWriter

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。

実行するタスク 参考例があるトピック
テキスト ファイルを作成する。 ファイルへのテキストの書き込み
テキスト ファイルに書き込む。 ファイルへのテキストの書き込み
テキスト ファイルから読み取る。 ファイルからのテキストの読み取り
テキストをファイルに追加する。 ログ ファイルのオープンと追加

File.AppendText

FileInfo.AppendText

ファイルのサイズを取得する。 FileInfo.Length
ファイルの属性を取得する。 File.GetAttributes
ファイルの属性を設定する。 File.SetAttributes
ファイルが存在するかどうかを判別する。 File.Exists
バイナリ ファイルから読み取る。 新しく作成したデータ ファイルの読み取りと書き込み
バイナリ ファイルに書き込む。 新しく作成したデータ ファイルの読み取りと書き込み

使用例

[Visual Basic, C#, C++] 次に示すのは、複数のセンテンスを含むテキストから、各センテンスがそれぞれ 2 つのスペースで区切られたパラグラフを作成し、その後そのパラグラフを元のテキストに戻すコード例です。

 
Option Explicit
Option Strict

Imports Microsoft.VisualBasic
Imports System
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

[C#] 
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 = 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());
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

void main()
{
    String* textReaderText = S"TextReader is the abstract base "
        S"class of StreamReader and StringReader, which read "
        S"characters from streams and strings, respectively.\n\n"

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

        S"You can also use an instance of TextReader to read "
        S"text from a custom backing store using the same "
        S"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;
    StringReader* strReader  = new StringReader(textReaderText);
    while(true)
    {
        aLine = strReader->ReadLine();
        if(aLine != 0)
        {
            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 = 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 = 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());
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.IO

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

StringWriter メンバ | System.IO 名前空間 | StringBuilder | StringReader | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み