다음을 통해 공유


Console.SetOut 메서드

Out 속성을 지정한 TextWriter 개체로 설정합니다.

네임스페이스: System
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Shared Sub SetOut ( _
    newOut As TextWriter _
)
‘사용 방법
Dim newOut As TextWriter

Console.SetOut(newOut)
public static void SetOut (
    TextWriter newOut
)
public:
static void SetOut (
    TextWriter^ newOut
)
public static void SetOut (
    TextWriter newOut
)
public static function SetOut (
    newOut : TextWriter
)

매개 변수

  • newOut
    새 표준 출력을 나타내는 TextWriter 스트림입니다.

예외

예외 형식 조건

ArgumentNullException

newOut이 Null 참조(Visual Basic의 경우 Nothing)인 경우

SecurityException

호출자에게 필요한 권한이 없는 경우

설명

기본적으로 Out 속성은 표준 출력 스트림으로 설정되어 있습니다.

FileStream을 캡슐화하는 StreamWriter는 파일에 출력을 보내는 데 사용할 수 있습니다. 예를 들면 다음과 같습니다.

Console.WriteLine("Hello World");
  FileStream fs1 = new FileStream("Test.txt", FileMode.Create);
  // First, save the standard output.
  TextWriter tmp = Console.Out;
  StreamWriter sw1 = new StreamWriter(fs1);
  Console.SetOut(sw1);
  Console.WriteLine("Hello file");
  Console.SetOut(tmp);
  Console.WriteLine("Hello World");
    sw1.Close();

예제

다음 코드 예제에서는 SetOut 메서드의 사용 방법을 설명합니다.

Public Class InsertTabs
   Private Const tabSize As Integer = 4
   Private Const usageText As String = "Usage: INSERTTABS inputfile.txt outputfile.txt"
   
   'Entry point which delegates to C-style main Private Function
   Public Overloads Shared Sub Main()
      System.Environment.ExitCode = Main(System.Environment.GetCommandLineArgs())
   End Sub
   
   Overloads Public Shared Function Main(args() As String) As Integer
      Dim writer As StreamWriter = Nothing
      
      If args.Length < 3 Then
         Console.WriteLine(usageText)
         Return 1
      End If
      
      Try
         writer = New StreamWriter(args(2))
         Console.SetOut(writer)
         Console.SetIn(New StreamReader(args(1)))
      Catch e As IOException
         Dim errorWriter As TextWriter = Console.Error
         errorWriter.WriteLine(e.Message)
         errorWriter.WriteLine(usageText)
         Return 1
      End Try
      Dim line As String
      line = Console.ReadLine()
      While Not line Is Nothing
         Dim newLine As String = line.Replace("".PadRight(tabSize, " "c), ControlChars.Tab)
         Console.WriteLine(newLine)
         line = Console.ReadLine()
      End While
      writer.Close()
      ' Recover the standard output stream so that a 
      ' completion message can be displayed.
      Dim standardOutput As New StreamWriter(Console.OpenStandardOutput())
      standardOutput.AutoFlush = True
      Console.SetOut(standardOutput)
      Console.WriteLine("INSERTTABS has completed the processing of {0}.", args(0))
      Return 0
   End Function 'Main
End Class 'InsertTabs
public class InsertTabs {
    private const int tabSize = 4;
    private const string usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";
    public static int Main(string[] args) {
        StreamWriter writer = null;

        if (args.Length < 2) {
            Console.WriteLine(usageText);
            return 1;
        }

        try {
            writer = new StreamWriter(args[1]);
            Console.SetOut(writer);
            Console.SetIn(new StreamReader(args[0]));
        }
        catch(IOException e) {
            TextWriter errorWriter = Console.Error;
            errorWriter.WriteLine(e.Message);
            errorWriter.WriteLine(usageText);
            return 1;            
        }
        string line;
        while ((line = Console.ReadLine()) != null) {
            string newLine = line.Replace(("").PadRight(tabSize, ' '), "\t");
            Console.WriteLine(newLine);
        }
        writer.Close();
        // Recover the standard output stream so that a 
        // completion message can be displayed.
        StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput());
        standardOutput.AutoFlush = true;
        Console.SetOut(standardOutput);
        Console.WriteLine("INSERTTABS has completed the processing of {0}.", args[0]);
        return 0;
    }
}
int main()
{
   array<String^>^args = Environment::GetCommandLineArgs();
   const int tabSize = 4;
   String^ usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";
   StreamWriter^ writer = nullptr;
   if ( args->Length < 3 )
   {
      Console::WriteLine( usageText );
      return 1;
   }

   try
   {
      writer = gcnew StreamWriter( args[ 2 ] );
      Console::SetOut( writer );
      Console::SetIn( gcnew StreamReader( args[ 1 ] ) );
   }
   catch ( IOException^ e ) 
   {
      TextWriter^ errorWriter = Console::Error;
      errorWriter->WriteLine( e->Message );
      errorWriter->WriteLine( usageText );
      return 1;
   }

   String^ line;
   while ( (line = Console::ReadLine()) != nullptr )
   {
      String^ newLine = line->Replace( ((String^)"")->PadRight( tabSize, ' ' ), "\t" );
      Console::WriteLine( newLine );
   }

   writer->Close();
   
   // Recover the standard output stream so that a 
   // completion message can be displayed.
   StreamWriter^ standardOutput = gcnew StreamWriter( Console::OpenStandardOutput() );
   standardOutput->AutoFlush = true;
   Console::SetOut( standardOutput );
   Console::WriteLine( "INSERTTABS has completed the processing of {0}.", args[ 1 ] );
   return 0;
}
public class InsertTabs
{
    private static int tabSize = 4;
    private static String usageText = "Usage: INSERTTABS inputfile.txt"
        + " outputfile.txt";
    
    public static void main(String[] args)
    {
        StreamWriter writer = null;

        if (args.length < 2) {        
            Console.WriteLine(usageText);
            return ;
        }

        try {        
            writer = new StreamWriter(args[1]);
            Console.SetOut(writer);
            Console.SetIn(new StreamReader(args[0]));
        }
        catch (IOException e) {        
            TextWriter errorWriter = Console.get_Error();
            errorWriter.WriteLine(e.get_Message());
            errorWriter.WriteLine(usageText);
            return ;
        }

        String line;
        while (((line = Console.ReadLine()) != null)) {        
            String newLine = line.Replace("".PadRight(tabSize, ' '), "\t");
            Console.WriteLine(newLine);
        }

        writer.Close();

        // Recover the standard output stream so that a 
        // completion message can be displayed.
        StreamWriter standardOutput = new StreamWriter(Console.
            OpenStandardOutput());
        standardOutput.set_AutoFlush(true);
        Console.SetOut(standardOutput);
        Console.WriteLine("INSERTTABS has completed the processing of {0}.", 
            args[0]);    
    } //main
} //InsertTabs
const tabSize = 4;
const usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";

var writer : StreamWriter = null;
var args = Environment.GetCommandLineArgs();

if (args.Length != 3) {
    Console.WriteLine(usageText);
    Environment.Exit(1);
}

try {
    writer = new StreamWriter(args[2]);
    Console.SetOut(writer);
    Console.SetIn(new StreamReader(args[1]));
}
catch(e : IOException) {
    var errorWriter = Console.Error;
    errorWriter.WriteLine(e.Message);
    errorWriter.WriteLine(usageText);
    Environment.Exit(1);            
}
var line;
while ((line = Console.ReadLine()) != null) {
    var newLine = line.Replace(("").PadRight(tabSize, ' '), "\t");
    Console.WriteLine(newLine);
}
writer.Close();
// Recover the standard output stream so that a 
// completion message can be displayed.
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
Console.WriteLine("INSERTTABS has completed the processing of {0}.", args[0]);

.NET Framework 보안

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0에서 지원

참고 항목

참조

Console 클래스
Console 멤버
System 네임스페이스
TextWriter
Out