영어로 읽기

다음을 통해 공유


Console.SetIn(TextReader) 메서드

정의

In 속성을 지정한 TextReader 개체로 설정합니다.

[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static void SetIn(System.IO.TextReader newIn);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static void SetIn(System.IO.TextReader newIn);
public static void SetIn(System.IO.TextReader newIn);

매개 변수

newIn
TextReader

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

특성

예외

newIn이(가) null인 경우

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

예제

다음 예제에서는 SetIn 메서드를 사용하는 방법을 보여 줍니다. 탭 문자가 있는 문자열에서 4개의 연속된 공백 문자를 대체합니다. 실행하려면 두 개의 명령줄 인수를 제공해야 합니다. 첫 번째는 표준 입력 스트림을 리디렉션하려는 기존 텍스트 파일의 이름입니다. 두 번째는 표준 출력 스트림을 리디렉션하려는 파일의 이름입니다. 이 파일이 있을 필요는 없습니다. 그러한 경우 해당 내용은 덮어씁니다.

using System;
using System.IO;

public class InsertTabs
{
    private const int tabSize = 4;
    private const string usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";
    public static int Main(string[] args)
    {
        if (args.Length < 2)
        {
            Console.WriteLine(usageText);
            return 1;
        }

        try
        {
            // Attempt to open output file.
            using (var writer = new StreamWriter(args[1]))
            {
                using (var reader = new StreamReader(args[0]))
                {
                    // Redirect standard output from the console to the output file.
                    Console.SetOut(writer);
                    // Redirect standard input from the console to the input file.
                    Console.SetIn(reader);
                    string line;
                    while ((line = Console.ReadLine()) != null)
                    {
                        string newLine = line.Replace(("").PadRight(tabSize, ' '), "\t");
                        Console.WriteLine(newLine);
                    }
                }
            }
        }
        catch(IOException e)
        {
            TextWriter errorWriter = Console.Error;
            errorWriter.WriteLine(e.Message);
            errorWriter.WriteLine(usageText);
            return 1;
        }

        // 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 {args[0]}.");
        return 0;
    }
}

설명

기본적으로 In 속성은 표준 입력 스트림으로 설정됩니다.

StreamReader 를 캡슐화하는 을 FileStream 사용하여 파일에서 입력을 받을 수 있습니다.

적용 대상

제품 버전
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

추가 정보