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 를 사용하면 문자열에 동기적으로 또는 비동기적으로 쓸 수 있습니다. 또는 메서드를 사용하여 한 번에 Write(Char) 문자, 또는 WriteAsync(Char) 메서드를 사용하여 한 번에 문자열을 작성할 Write(String)WriteAsync(String) 수 있습니다. 또한 문자, 문자 배열 또는 문자열 뒤에 메서드 중 WriteLineAsync 하나를 사용하여 줄 종결자를 비동기적으로 작성할 수 있습니다.

참고

이 형식은 IDisposable 인터페이스를 구현하지만 실제로 삭제하는 리소스는 없습니다. 즉 Dispose()를 직접 호출하거나 using(C#) 또는 Using(Visual Basic) 같은 언어 구문을 사용하여 삭제할 필요가 없습니다.

다음 표에는 다른 일반적인 또는 관련 I/O 작업의 예가 나와 있습니다.

수행할 작업 이 항목의 예제를 참조하세요.
텍스트 파일을 만듭니다. 방법: 파일에 텍스트 쓰기
텍스트 파일에 씁니다. 방법: 파일에 텍스트 쓰기
텍스트 파일에서 읽습니다. 방법: 파일에서 텍스트 읽기
파일에 텍스트를 추가합니다. 방법: 로그 파일 열기 및 추가

File.AppendText

FileInfo.AppendText
파일의 크기를 가져옵니다. FileInfo.Length
파일의 특성을 가져옵니다. File.GetAttributes
파일의 특성을 설정합니다. File.SetAttributes
파일이 있는지 확인합니다. File.Exists
이진 파일에서 읽습니다. 방법: 새로 만든 데이터 파일 읽기 및 쓰기
이진 파일에 씁니다. 방법: 새로 만든 데이터 파일 읽기 및 쓰기

생성자

StringWriter()

StringWriter 클래스의 새 인스턴스를 초기화합니다.

StringWriter(IFormatProvider)

지정된 형식 컨트롤을 사용하여 StringWriter 클래스의 새 인스턴스를 초기화합니다.

StringWriter(StringBuilder)

지정된 StringWriter 에 쓰는 StringBuilder클래스의 새 인스턴스를 초기화합니다.

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)

10진수 값의 텍스트 표현을 텍스트 스트림에 씁니다.

(다음에서 상속됨 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)

10진수 값의 텍스트 표현과 줄 종결자를 차례로 텍스트 스트림에 씁니다.

(다음에서 상속됨 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)

비동기 일회용에서 반환되는 작업을 대기하는 방법을 구성합니다.

적용 대상

추가 정보