StreamReader 생성자

정의

지정된 스트림에 대한 StreamReader 클래스의 새 인스턴스를 초기화합니다.

오버로드

StreamReader(Stream)

지정된 스트림에 대한 StreamReader 클래스의 새 인스턴스를 초기화합니다.

StreamReader(Stream, Encoding, Boolean, Int32, Boolean)

지정된 문자 인코딩과 바이트 순서 표시 검색 옵션, 버퍼 크기를 기반으로 지정된 스트림에 대해 StreamReader 클래스의 새 인스턴스를 초기화하고 스트림을 선택적으로 연 상태로 둡니다.

StreamReader(String, Encoding, Boolean, FileStreamOptions)

지정된 문자 인코딩, 바이트 순서 표시 검색 옵션을 사용하여 지정된 파일 경로에 대한 클래스의 StreamReader 새 instance 초기화하고 지정된 개체로 구성합니다FileStreamOptions.

StreamReader(String, Encoding, Boolean, Int32)

지정된 문자 인코딩과 바이트 순서 표시 검색 옵션을 사용하여 지정된 파일 이름에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

StreamReader(Stream, Encoding, Boolean, Int32)

지정된 문자 인코딩과 바이트 순서 표시 검색 옵션, 버퍼 크기를 사용하여 특정 스트림에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

StreamReader(Stream, Encoding, Boolean)

지정된 문자 인코딩과 바이트 순서 표시 검색 옵션을 사용하여 특정 스트림에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

StreamReader(String, Encoding, Boolean)

지정한 문자 인코딩과 바이트 순서 표시 검색 옵션을 사용하여 지정된 파일 이름에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

StreamReader(String, FileStreamOptions)

기본 인코딩을 사용하여 지정된 파일 경로에 대한 클래스의 StreamReader 새 instance 초기화하여 파일 시작 부분에서 바이트 순서 표시를 검색하고 지정된 개체로 구성합니다FileStreamOptions.

StreamReader(String, Boolean)

지정한 바이트 순서 표시 검색 옵션을 사용하여 지정한 파일 이름에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

StreamReader(Stream, Encoding)

지정된 문자 인코딩을 사용하여 지정된 스트림에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

StreamReader(Stream, Boolean)

지정한 바이트 순서 표시 검색 옵션을 사용하여 지정된 스트림에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

StreamReader(String)

지정된 파일 이름에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

StreamReader(String, Encoding)

지정된 문자 인코딩을 사용하여 지정된 파일 이름에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

StreamReader(Stream)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

지정된 스트림에 대한 StreamReader 클래스의 새 인스턴스를 초기화합니다.

public:
 StreamReader(System::IO::Stream ^ stream);
public StreamReader (System.IO.Stream stream);
new System.IO.StreamReader : System.IO.Stream -> System.IO.StreamReader
Public Sub New (stream As Stream)

매개 변수

stream
Stream

읽을 스트림입니다.

예외

stream 이 읽기를 지원하지 않습니다.

stream이(가) null인 경우

예제

다음 코드 예제에서는이 StreamReader 생성자를 보여 줍니다.

using namespace System;
using namespace System::IO;
int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   try
   {
      if ( File::Exists( path ) )
      {
         File::Delete( path );
      }
      StreamWriter^ sw = gcnew StreamWriter( path );
      try
      {
         sw->WriteLine( "This" );
         sw->WriteLine( "is some text" );
         sw->WriteLine( "to test" );
         sw->WriteLine( "Reading" );
      }
      finally
      {
         delete sw;
      }

      FileStream^ fs = gcnew FileStream( path,FileMode::Open );
      try
      {
         StreamReader^ sr = gcnew StreamReader( fs );
         try
         {
            while ( sr->Peek() >= 0 )
            {
               Console::WriteLine( sr->ReadLine() );
            }
         }
         finally
         {
            delete sr;
         }
      }
      finally
      {
         delete fs;
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }
}
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        try
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }

            using (StreamWriter sw = new StreamWriter(new FileStream(path, FileMode.CreateNew)))
            {
                sw.WriteLine("This");
                sw.WriteLine("is some text");
                sw.WriteLine("to test");
                sw.WriteLine("reading");
            }

            using (StreamReader sr = new StreamReader(new FileStream(path, FileMode.Open)))
            {
                while (sr.Peek() >= 0)
                {
                    Console.WriteLine(sr.ReadLine());
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
Imports System.IO

Public Class Test

    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        Try
            If File.Exists(path) Then
                File.Delete(path)
            End If

            Dim sw As StreamWriter = New StreamWriter(path)
            sw.WriteLine("This")
            sw.WriteLine("is some text")
            sw.WriteLine("to test")
            sw.WriteLine("Reading")
            sw.Close()

            Dim fs As FileStream = New FileStream(path, FileMode.Open)

            Dim sr As StreamReader = New StreamReader(fs)

            Do While sr.Peek() >= 0
                Console.WriteLine(sr.ReadLine())
            Loop
            sr.Close()
            fs.Close()
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

설명

이 생성자는 인코딩을 UTF8EncodingBaseStream , 매개 변수를 사용하는 stream 속성 및 내부 버퍼 크기를 1024바이트로 초기화합니다.

개체는 StreamReader 가 호출될 때 StreamReader.Dispose 제공된 Stream 개체에서 를 호출 Dispose() 합니다.

주의

특정 문화권 설정을 사용하여 문자 집합을 컴파일하고 다른 문화권 설정으로 동일한 문자를 검색하는 경우 문자를 해석할 수 없으며 예외가 throw될 수 있습니다.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

추가 정보

적용 대상

StreamReader(Stream, Encoding, Boolean, Int32, Boolean)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

지정된 문자 인코딩과 바이트 순서 표시 검색 옵션, 버퍼 크기를 기반으로 지정된 스트림에 대해 StreamReader 클래스의 새 인스턴스를 초기화하고 스트림을 선택적으로 연 상태로 둡니다.

public:
 StreamReader(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen);
public StreamReader (System.IO.Stream stream, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen);
public StreamReader (System.IO.Stream stream, System.Text.Encoding? encoding = default, bool detectEncodingFromByteOrderMarks = true, int bufferSize = -1, bool leaveOpen = false);
new System.IO.StreamReader : System.IO.Stream * System.Text.Encoding * bool * int * bool -> System.IO.StreamReader
Public Sub New (stream As Stream, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, bufferSize As Integer, leaveOpen As Boolean)
Public Sub New (stream As Stream, Optional encoding As Encoding = Nothing, Optional detectEncodingFromByteOrderMarks As Boolean = true, Optional bufferSize As Integer = -1, Optional leaveOpen As Boolean = false)

매개 변수

stream
Stream

읽을 스트림입니다.

encoding
Encoding

사용할 문자 인코딩입니다.

detectEncodingFromByteOrderMarks
Boolean

파일의 시작 부분에서 바이트 순서 표시를 찾으려면true 이고, 찾지 않으려면 false입니다.

bufferSize
Int32

최소 버퍼 크기입니다.

leaveOpen
Boolean

StreamReader 개체를 삭제한 후 스트림을 열어 두려면 true이고, 닫으려면 false입니다.

설명

매개 변수를 leaveOpen 로 설정하지 않는 한 개체는 StreamReader 가 호출될 때 StreamReader.Dispose 제공된 Stream 개체에서 를 호출 Dispose()true합니다.

버퍼 크기(16비트 문자 수)는 매개 변수에 bufferSize 의해 설정됩니다. 가 허용 가능한 최소 크기(128자)보다 작은 경우 bufferSize 허용되는 최소 크기가 사용됩니다.

이 생성자를 사용하면 개체에서 StreamReader 처음 읽을 때 인코딩을 변경할 수 있습니다. 매개 변수가 detectEncodingFromByteOrderMarkstrue인 경우 생성자는 스트림의 처음 4바이트를 확인하여 인코딩을 검색합니다. 파일이 적절한 바이트 순서 표시로 시작하는 경우 UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32 및 big-endian UTF-32 텍스트를 자동으로 인식합니다. 그렇지 않으면 사용자가 제공한 인코딩이 사용됩니다. 자세한 내용은 Encoding.GetPreamble 메서드를 참조하세요.

참고

에서 Stream읽을 때 스트림의 내부 버퍼와 크기가 같은 버퍼를 사용하는 것이 더 효율적입니다.

주의

특정 문화권 설정을 사용하여 문자 집합을 컴파일하고 다른 문화권 설정으로 동일한 문자를 검색하는 경우 문자가 올바르게 해석되지 않아 예외가 throw될 수 있습니다.

적용 대상

StreamReader(String, Encoding, Boolean, FileStreamOptions)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

지정된 문자 인코딩, 바이트 순서 표시 검색 옵션을 사용하여 지정된 파일 경로에 대한 클래스의 StreamReader 새 instance 초기화하고 지정된 개체로 구성합니다FileStreamOptions.

public:
 StreamReader(System::String ^ path, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks, System::IO::FileStreamOptions ^ options);
public StreamReader (string path, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks, System.IO.FileStreamOptions options);
new System.IO.StreamReader : string * System.Text.Encoding * bool * System.IO.FileStreamOptions -> System.IO.StreamReader
Public Sub New (path As String, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, options As FileStreamOptions)

매개 변수

path
String

읽을 전체 파일 경로입니다.

encoding
Encoding

사용할 문자 인코딩입니다.

detectEncodingFromByteOrderMarks
Boolean

파일의 시작 부분에서 바이트 순서 표시를 찾을지 여부를 나타냅니다.

options
FileStreamOptions

기본 에 대한 구성 옵션을 지정하는 개체입니다 FileStream.

예외

stream을 읽을 수 없습니다.

      -or-

path가 빈 문자열("")인 경우

path, encoding 또는 optionsnull인 경우

파일을 찾을 수 없습니다.

지정된 경로가 잘못되었습니다(예: 매핑되지 않은 드라이브에 있음).

path 에 파일 이름, 디렉터리 이름 또는 볼륨 레이블에 대해 올바르지 않거나 잘못된 구문이 포함되어 있습니다.

추가 정보

적용 대상

StreamReader(String, Encoding, Boolean, Int32)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

지정된 문자 인코딩과 바이트 순서 표시 검색 옵션을 사용하여 지정된 파일 이름에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

public:
 StreamReader(System::String ^ path, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
public StreamReader (string path, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
new System.IO.StreamReader : string * System.Text.Encoding * bool * int -> System.IO.StreamReader
Public Sub New (path As String, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, bufferSize As Integer)

매개 변수

path
String

읽을 전체 파일 경로입니다.

encoding
Encoding

사용할 문자 인코딩입니다.

detectEncodingFromByteOrderMarks
Boolean

파일의 시작 부분에서 바이트 순서 표시를 찾을지 여부를 나타냅니다.

bufferSize
Int32

최소 버퍼 크기(16비트 문자 수)입니다.

예외

path가 빈 문자열("")인 경우

path 또는 encodingnull인 경우

파일을 찾을 수 없습니다.

지정된 경로가 잘못되었습니다(예: 매핑되지 않은 드라이브에 있음).

path 에 파일 이름, 디렉터리 이름 또는 볼륨 레이블에 대해 올바르지 않거나 잘못된 구문이 포함되어 있습니다.

buffersize가 0보다 작거나 같습니다.

예제

다음 코드 예제에서는이 StreamReader 생성자를 보여 줍니다.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false,512 );
   
   //Get a new StreamReader in ASCII format from a
   //file with byte order mark detection = false
   StreamReader^ srAsciiFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt" );
   
   //Get a new StreamReader in ASCII format from a
   //FileStream with byte order mark detection = false and a buffer
   StreamReader^ srAsciiFromStreamFalse512 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false,512 );
   
   //Get a new StreamReader in ASCII format from a
   //FileStream with byte order mark detection = false
   StreamReader^ srAsciiFromStreamFalse = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //FileStream with byte order mark detection = false
   StreamReader^ srFromStreamFalse = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}
private void getNewStreamReader()
{
    //Get a new StreamReader in ASCII format from a
    //file using a buffer and byte order mark detection
    StreamReader srAsciiFromFileFalse512 =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //file with byte order mark detection = false
    StreamReader srAsciiFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a file
    StreamReader srAsciiFromFile =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //file with byte order mark detection = false
    StreamReader srFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt", false);
    //Get a new StreamReader from a file
    StreamReader srFromFile =
        new StreamReader("C:\\Temp\\Test.txt");
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false and a buffer
    StreamReader srAsciiFromStreamFalse512 = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false
    StreamReader srAsciiFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a FileStream
    StreamReader srAsciiFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //FileStream with byte order mark detection = false
    StreamReader srFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        false);
    //Get a new StreamReader from a FileStream
    StreamReader srFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
}
Private Sub getNewStreamReader()
    Dim S As Stream = File.OpenRead("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'file using a buffer and byte order mark detection
    Dim SrAsciiFromFileFalse512 As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'file with byte order mark detection = false
    Dim SrAsciiFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a file 
    Dim SrAsciiFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'file with byte order mark detection = false        
    Dim SrFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", False)
    'Get a new StreamReader from a file
    Dim SrFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false and a buffer
    Dim SrAsciiFromStreamFalse512 As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false
    Dim SrAsciiFromStreamFalse = New StreamReader(S, _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a FileStream
    Dim SrAsciiFromStream As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'FileStream with byte order mark detection = false
    Dim SrFromStreamFalse As StreamReader = New StreamReader(S, False)
    'Get a new StreamReader from a FileStream
    Dim SrFromStream As StreamReader = New StreamReader(S)

End Sub

설명

이 생성자는 매개 변수에 지정된 대로 인코딩을 encoding 초기화합니다.

이 생성자를 사용하면 개체에서 StreamReader 처음 읽을 때 인코딩을 변경할 수 있습니다. 매개 변수는 detectEncodingFromByteOrderMarks 스트림의 처음 4바이트를 확인하여 인코딩을 검색합니다. 파일이 적절한 바이트 순서 표시로 시작하는 경우 UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32 및 big-endian UTF-32 텍스트를 자동으로 인식합니다. 그렇지 않으면 사용자가 제공한 인코딩이 사용됩니다. 자세한 내용은 Encoding.GetPreamble 메서드를 참조하세요.

버퍼 크기(16비트 문자 수)는 매개 변수에 bufferSize 의해 설정됩니다. 가 허용 가능한 최소 크기(128자)보다 작은 경우 bufferSize 허용되는 최소 크기가 사용됩니다.

매개 변수는 path UNC(범용 명명 규칙) 공유의 파일을 포함하여 파일 이름이 될 수 있습니다.

매개 변수는 path 디스크에 저장된 파일일 필요는 없습니다. 스트림을 사용하여 액세스를 지원하는 시스템의 일부일 수 있습니다.

주의

특정 문화권 설정을 사용하여 문자 집합을 컴파일하고 다른 문화권 설정으로 동일한 문자를 검색하는 경우 문자를 해석할 수 없으며 예외가 throw될 수 있습니다.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

추가 정보

적용 대상

StreamReader(Stream, Encoding, Boolean, Int32)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

지정된 문자 인코딩과 바이트 순서 표시 검색 옵션, 버퍼 크기를 사용하여 특정 스트림에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

public:
 StreamReader(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
public StreamReader (System.IO.Stream stream, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
new System.IO.StreamReader : System.IO.Stream * System.Text.Encoding * bool * int -> System.IO.StreamReader
Public Sub New (stream As Stream, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, bufferSize As Integer)

매개 변수

stream
Stream

읽을 스트림입니다.

encoding
Encoding

사용할 문자 인코딩입니다.

detectEncodingFromByteOrderMarks
Boolean

파일의 시작 부분에서 바이트 순서 표시를 찾을지 여부를 나타냅니다.

bufferSize
Int32

최소 버퍼 크기입니다.

예외

스트림이 읽기를 지원하지 않습니다.

stream 또는 encodingnull인 경우

bufferSize가 0보다 작거나 같습니다.

예제

다음 코드 예제에서는이 StreamReader 생성자를 보여 줍니다.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false,512 );
   
   //Get a new StreamReader in ASCII format from a
   //file with byte order mark detection = false
   StreamReader^ srAsciiFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt" );
   
   //Get a new StreamReader in ASCII format from a
   //FileStream with byte order mark detection = false and a buffer
   StreamReader^ srAsciiFromStreamFalse512 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false,512 );
   
   //Get a new StreamReader in ASCII format from a
   //FileStream with byte order mark detection = false
   StreamReader^ srAsciiFromStreamFalse = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //FileStream with byte order mark detection = false
   StreamReader^ srFromStreamFalse = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}
private void getNewStreamReader()
{
    //Get a new StreamReader in ASCII format from a
    //file using a buffer and byte order mark detection
    StreamReader srAsciiFromFileFalse512 =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //file with byte order mark detection = false
    StreamReader srAsciiFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a file
    StreamReader srAsciiFromFile =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //file with byte order mark detection = false
    StreamReader srFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt", false);
    //Get a new StreamReader from a file
    StreamReader srFromFile =
        new StreamReader("C:\\Temp\\Test.txt");
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false and a buffer
    StreamReader srAsciiFromStreamFalse512 = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false
    StreamReader srAsciiFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a FileStream
    StreamReader srAsciiFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //FileStream with byte order mark detection = false
    StreamReader srFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        false);
    //Get a new StreamReader from a FileStream
    StreamReader srFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
}
Private Sub getNewStreamReader()
    Dim S As Stream = File.OpenRead("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'file using a buffer and byte order mark detection
    Dim SrAsciiFromFileFalse512 As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'file with byte order mark detection = false
    Dim SrAsciiFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a file 
    Dim SrAsciiFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'file with byte order mark detection = false        
    Dim SrFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", False)
    'Get a new StreamReader from a file
    Dim SrFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false and a buffer
    Dim SrAsciiFromStreamFalse512 As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false
    Dim SrAsciiFromStreamFalse = New StreamReader(S, _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a FileStream
    Dim SrAsciiFromStream As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'FileStream with byte order mark detection = false
    Dim SrFromStreamFalse As StreamReader = New StreamReader(S, False)
    'Get a new StreamReader from a FileStream
    Dim SrFromStream As StreamReader = New StreamReader(S)

End Sub

설명

버퍼 크기(16비트 문자 수)는 매개 변수에 bufferSize 의해 설정됩니다. 가 허용 가능한 최소 크기(128자)보다 작은 경우 bufferSize 허용되는 최소 크기가 사용됩니다.

이 생성자를 사용하면 개체에서 StreamReader 처음 읽을 때 인코딩을 변경할 수 있습니다. 매개 변수는 detectEncodingFromByteOrderMarks 스트림의 처음 4바이트를 확인하여 인코딩을 검색합니다. 파일이 적절한 바이트 순서 표시로 시작하는 경우 UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32 및 big-endian UTF-32 텍스트를 자동으로 인식합니다. 그렇지 않으면 사용자가 제공한 인코딩이 사용됩니다. 자세한 내용은 Encoding.GetPreamble 메서드를 참조하세요.

개체는 StreamReader 가 호출될 때 StreamReader.Dispose 제공된 Stream 개체에서 를 호출 Dispose() 합니다.

참고

에서 Stream읽을 때 스트림의 내부 버퍼와 크기가 같은 버퍼를 사용하는 것이 더 효율적입니다.

주의

특정 문화권 설정을 사용하여 문자 집합을 컴파일하고 다른 문화권 설정으로 동일한 문자를 검색하는 경우 문자를 해석할 수 없으며 예외가 throw될 수 있습니다.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

추가 정보

적용 대상

StreamReader(Stream, Encoding, Boolean)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

지정된 문자 인코딩과 바이트 순서 표시 검색 옵션을 사용하여 특정 스트림에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

public:
 StreamReader(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks);
public StreamReader (System.IO.Stream stream, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks);
new System.IO.StreamReader : System.IO.Stream * System.Text.Encoding * bool -> System.IO.StreamReader
Public Sub New (stream As Stream, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean)

매개 변수

stream
Stream

읽을 스트림입니다.

encoding
Encoding

사용할 문자 인코딩입니다.

detectEncodingFromByteOrderMarks
Boolean

파일의 시작 부분에서 바이트 순서 표시를 찾을지 여부를 나타냅니다.

예외

stream 이 읽기를 지원하지 않습니다.

stream 또는 encodingnull인 경우

예제

다음 코드 예제에서는이 StreamReader 생성자를 보여 줍니다.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false,512 );
   
   //Get a new StreamReader in ASCII format from a
   //file with byte order mark detection = false
   StreamReader^ srAsciiFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt" );
   
   //Get a new StreamReader in ASCII format from a
   //FileStream with byte order mark detection = false and a buffer
   StreamReader^ srAsciiFromStreamFalse512 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false,512 );
   
   //Get a new StreamReader in ASCII format from a
   //FileStream with byte order mark detection = false
   StreamReader^ srAsciiFromStreamFalse = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //FileStream with byte order mark detection = false
   StreamReader^ srFromStreamFalse = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}
private void getNewStreamReader()
{
    //Get a new StreamReader in ASCII format from a
    //file using a buffer and byte order mark detection
    StreamReader srAsciiFromFileFalse512 =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //file with byte order mark detection = false
    StreamReader srAsciiFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a file
    StreamReader srAsciiFromFile =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //file with byte order mark detection = false
    StreamReader srFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt", false);
    //Get a new StreamReader from a file
    StreamReader srFromFile =
        new StreamReader("C:\\Temp\\Test.txt");
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false and a buffer
    StreamReader srAsciiFromStreamFalse512 = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false
    StreamReader srAsciiFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a FileStream
    StreamReader srAsciiFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //FileStream with byte order mark detection = false
    StreamReader srFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        false);
    //Get a new StreamReader from a FileStream
    StreamReader srFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
}
Private Sub getNewStreamReader()
    Dim S As Stream = File.OpenRead("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'file using a buffer and byte order mark detection
    Dim SrAsciiFromFileFalse512 As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'file with byte order mark detection = false
    Dim SrAsciiFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a file 
    Dim SrAsciiFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'file with byte order mark detection = false        
    Dim SrFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", False)
    'Get a new StreamReader from a file
    Dim SrFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false and a buffer
    Dim SrAsciiFromStreamFalse512 As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false
    Dim SrAsciiFromStreamFalse = New StreamReader(S, _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a FileStream
    Dim SrAsciiFromStream As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'FileStream with byte order mark detection = false
    Dim SrFromStreamFalse As StreamReader = New StreamReader(S, False)
    'Get a new StreamReader from a FileStream
    Dim SrFromStream As StreamReader = New StreamReader(S)

End Sub

설명

이 생성자는 매개 변수에 지정된 encoding 인코딩, 매개 변수를 BaseStream 사용하는 stream 속성 및 내부 버퍼 크기를 1024바이트로 초기화합니다.

매개 변수는 detectEncodingFromByteOrderMarks 스트림의 처음 4바이트를 확인하여 인코딩을 검색합니다. 파일이 적절한 바이트 순서 표시로 시작하는 경우 UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32 및 big-endian UTF-32 텍스트를 자동으로 인식합니다. 그렇지 않으면 사용자가 제공한 인코딩이 사용됩니다. 자세한 내용은 Encoding.GetPreamble 메서드를 참조하세요.

개체는 StreamReader 가 호출될 때 StreamReader.Dispose 제공된 Stream 개체에서 를 호출 Dispose() 합니다.

주의

특정 문화권 설정을 사용하여 문자 집합을 컴파일하고 다른 문화권 설정으로 동일한 문자를 검색하는 경우 문자를 해석할 수 없으며 예외가 throw될 수 있습니다.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

추가 정보

적용 대상

StreamReader(String, Encoding, Boolean)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

지정한 문자 인코딩과 바이트 순서 표시 검색 옵션을 사용하여 지정된 파일 이름에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

public:
 StreamReader(System::String ^ path, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks);
public StreamReader (string path, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks);
new System.IO.StreamReader : string * System.Text.Encoding * bool -> System.IO.StreamReader
Public Sub New (path As String, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean)

매개 변수

path
String

읽을 전체 파일 경로입니다.

encoding
Encoding

사용할 문자 인코딩입니다.

detectEncodingFromByteOrderMarks
Boolean

파일의 시작 부분에서 바이트 순서 표시를 찾을지 여부를 나타냅니다.

예외

path가 빈 문자열("")인 경우

path 또는 encodingnull인 경우

파일을 찾을 수 없습니다.

지정된 경로가 잘못되었습니다(예: 매핑되지 않은 드라이브에 있음).

path 에 파일 이름, 디렉터리 이름 또는 볼륨 레이블에 대해 올바르지 않거나 잘못된 구문이 포함되어 있습니다.

예제

다음 코드 예제에서는이 StreamReader 생성자를 보여 줍니다.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false,512 );
   
   //Get a new StreamReader in ASCII format from a
   //file with byte order mark detection = false
   StreamReader^ srAsciiFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt" );
   
   //Get a new StreamReader in ASCII format from a
   //FileStream with byte order mark detection = false and a buffer
   StreamReader^ srAsciiFromStreamFalse512 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false,512 );
   
   //Get a new StreamReader in ASCII format from a
   //FileStream with byte order mark detection = false
   StreamReader^ srAsciiFromStreamFalse = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //FileStream with byte order mark detection = false
   StreamReader^ srFromStreamFalse = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}
private void getNewStreamReader()
{
    //Get a new StreamReader in ASCII format from a
    //file using a buffer and byte order mark detection
    StreamReader srAsciiFromFileFalse512 =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //file with byte order mark detection = false
    StreamReader srAsciiFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a file
    StreamReader srAsciiFromFile =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //file with byte order mark detection = false
    StreamReader srFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt", false);
    //Get a new StreamReader from a file
    StreamReader srFromFile =
        new StreamReader("C:\\Temp\\Test.txt");
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false and a buffer
    StreamReader srAsciiFromStreamFalse512 = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false
    StreamReader srAsciiFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a FileStream
    StreamReader srAsciiFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //FileStream with byte order mark detection = false
    StreamReader srFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        false);
    //Get a new StreamReader from a FileStream
    StreamReader srFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
}
Private Sub getNewStreamReader()
    Dim S As Stream = File.OpenRead("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'file using a buffer and byte order mark detection
    Dim SrAsciiFromFileFalse512 As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'file with byte order mark detection = false
    Dim SrAsciiFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a file 
    Dim SrAsciiFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'file with byte order mark detection = false        
    Dim SrFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", False)
    'Get a new StreamReader from a file
    Dim SrFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false and a buffer
    Dim SrAsciiFromStreamFalse512 As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false
    Dim SrAsciiFromStreamFalse = New StreamReader(S, _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a FileStream
    Dim SrAsciiFromStream As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'FileStream with byte order mark detection = false
    Dim SrFromStreamFalse As StreamReader = New StreamReader(S, False)
    'Get a new StreamReader from a FileStream
    Dim SrFromStream As StreamReader = New StreamReader(S)

End Sub

설명

이 생성자는 매개 변수에 지정된 대로 인코딩을 encoding 초기화하고 내부 버퍼 크기를 1024바이트로 초기화합니다.

매개 변수는 detectEncodingFromByteOrderMarks 스트림의 처음 4바이트를 확인하여 인코딩을 검색합니다. 파일이 적절한 바이트 순서 표시로 시작하는 경우 UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32 및 big-endian UTF-32 텍스트를 자동으로 인식합니다. 그렇지 않으면 사용자가 제공한 인코딩이 사용됩니다. 자세한 내용은 Encoding.GetPreamble 메서드를 참조하세요.

매개 변수는 path UNC(범용 명명 규칙) 공유의 파일을 포함하여 파일 이름이 될 수 있습니다.

매개 변수는 path 디스크에 저장된 파일일 필요는 없습니다. 스트림을 사용하여 액세스를 지원하는 시스템의 일부일 수 있습니다.

주의

특정 문화권 설정을 사용하여 문자 집합을 컴파일하고 다른 문화권 설정으로 동일한 문자를 검색하는 경우 문자를 해석할 수 없으며 예외가 throw될 수 있습니다.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

추가 정보

적용 대상

StreamReader(String, FileStreamOptions)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

기본 인코딩을 사용하여 지정된 파일 경로에 대한 클래스의 StreamReader 새 instance 초기화하여 파일 시작 부분에서 바이트 순서 표시를 검색하고 지정된 개체로 구성합니다FileStreamOptions.

public:
 StreamReader(System::String ^ path, System::IO::FileStreamOptions ^ options);
public StreamReader (string path, System.IO.FileStreamOptions options);
new System.IO.StreamReader : string * System.IO.FileStreamOptions -> System.IO.StreamReader
Public Sub New (path As String, options As FileStreamOptions)

매개 변수

path
String

읽을 전체 파일 경로입니다.

options
FileStreamOptions

기본 에 대한 구성 옵션을 지정하는 개체입니다 FileStream.

예외

stream을 읽을 수 없습니다.

또는

      <code data-dev-comment-type="paramref">path</code> is an empty string ("").

path 또는 optionsnull인 경우

파일을 찾을 수 없습니다.

지정된 경로가 잘못되었습니다(예: 매핑되지 않은 드라이브에 있음).

path 에 파일 이름, 디렉터리 이름 또는 볼륨 레이블에 대해 올바르지 않거나 잘못된 구문이 포함되어 있습니다.

추가 정보

적용 대상

StreamReader(String, Boolean)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

지정한 바이트 순서 표시 검색 옵션을 사용하여 지정한 파일 이름에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

public:
 StreamReader(System::String ^ path, bool detectEncodingFromByteOrderMarks);
public StreamReader (string path, bool detectEncodingFromByteOrderMarks);
new System.IO.StreamReader : string * bool -> System.IO.StreamReader
Public Sub New (path As String, detectEncodingFromByteOrderMarks As Boolean)

매개 변수

path
String

읽을 전체 파일 경로입니다.

detectEncodingFromByteOrderMarks
Boolean

파일의 시작 부분에서 바이트 순서 표시를 찾을지 여부를 나타냅니다.

예외

path가 빈 문자열("")인 경우

path이(가) null인 경우

파일을 찾을 수 없습니다.

지정된 경로가 잘못되었습니다(예: 매핑되지 않은 드라이브에 있음).

path 에 파일 이름, 디렉터리 이름 또는 볼륨 레이블에 대해 올바르지 않거나 잘못된 구문이 포함되어 있습니다.

예제

다음 코드 예제에서는이 StreamReader 생성자를 보여 줍니다.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false,512 );
   
   //Get a new StreamReader in ASCII format from a
   //file with byte order mark detection = false
   StreamReader^ srAsciiFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt" );
   
   //Get a new StreamReader in ASCII format from a
   //FileStream with byte order mark detection = false and a buffer
   StreamReader^ srAsciiFromStreamFalse512 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false,512 );
   
   //Get a new StreamReader in ASCII format from a
   //FileStream with byte order mark detection = false
   StreamReader^ srAsciiFromStreamFalse = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //FileStream with byte order mark detection = false
   StreamReader^ srFromStreamFalse = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}
private void getNewStreamReader()
{
    //Get a new StreamReader in ASCII format from a
    //file using a buffer and byte order mark detection
    StreamReader srAsciiFromFileFalse512 =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //file with byte order mark detection = false
    StreamReader srAsciiFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a file
    StreamReader srAsciiFromFile =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //file with byte order mark detection = false
    StreamReader srFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt", false);
    //Get a new StreamReader from a file
    StreamReader srFromFile =
        new StreamReader("C:\\Temp\\Test.txt");
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false and a buffer
    StreamReader srAsciiFromStreamFalse512 = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false
    StreamReader srAsciiFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a FileStream
    StreamReader srAsciiFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //FileStream with byte order mark detection = false
    StreamReader srFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        false);
    //Get a new StreamReader from a FileStream
    StreamReader srFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
}
Private Sub getNewStreamReader()
    Dim S As Stream = File.OpenRead("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'file using a buffer and byte order mark detection
    Dim SrAsciiFromFileFalse512 As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'file with byte order mark detection = false
    Dim SrAsciiFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a file 
    Dim SrAsciiFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'file with byte order mark detection = false        
    Dim SrFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", False)
    'Get a new StreamReader from a file
    Dim SrFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false and a buffer
    Dim SrAsciiFromStreamFalse512 As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false
    Dim SrAsciiFromStreamFalse = New StreamReader(S, _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a FileStream
    Dim SrAsciiFromStream As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'FileStream with byte order mark detection = false
    Dim SrFromStreamFalse As StreamReader = New StreamReader(S, False)
    'Get a new StreamReader from a FileStream
    Dim SrFromStream As StreamReader = New StreamReader(S)

End Sub

설명

이 생성자는 인코딩을 UTF8EncodingBaseStream , 매개 변수를 사용하는 stream 속성 및 내부 버퍼 크기를 1024바이트로 초기화합니다.

매개 변수는 path UNC(범용 명명 규칙) 공유의 파일을 포함하여 파일 이름이 될 수 있습니다.

매개 변수는 path 디스크에 저장된 파일일 필요는 없습니다. 스트림을 사용하여 액세스를 지원하는 시스템의 일부일 수 있습니다.

매개 변수는 detectEncodingFromByteOrderMarks 스트림의 처음 4바이트를 확인하여 인코딩을 검색합니다. 파일이 적절한 바이트 순서 표시로 시작하는 경우 UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32 및 big-endian UTF-32 텍스트를 자동으로 인식합니다. 그렇지 않으면 가 UTF8Encoding 사용됩니다. 자세한 내용은 Encoding.GetPreamble 메서드를 참조하세요.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

추가 정보

적용 대상

StreamReader(Stream, Encoding)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

지정된 문자 인코딩을 사용하여 지정된 스트림에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

public:
 StreamReader(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding);
public StreamReader (System.IO.Stream stream, System.Text.Encoding encoding);
new System.IO.StreamReader : System.IO.Stream * System.Text.Encoding -> System.IO.StreamReader
Public Sub New (stream As Stream, encoding As Encoding)

매개 변수

stream
Stream

읽을 스트림입니다.

encoding
Encoding

사용할 문자 인코딩입니다.

예외

stream 이 읽기를 지원하지 않습니다.

stream 또는 encodingnull인 경우

예제

다음 코드 예제에서는이 StreamReader 생성자를 보여 줍니다.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false,512 );
   
   //Get a new StreamReader in ASCII format from a
   //file with byte order mark detection = false
   StreamReader^ srAsciiFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt" );
   
   //Get a new StreamReader in ASCII format from a
   //FileStream with byte order mark detection = false and a buffer
   StreamReader^ srAsciiFromStreamFalse512 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false,512 );
   
   //Get a new StreamReader in ASCII format from a
   //FileStream with byte order mark detection = false
   StreamReader^ srAsciiFromStreamFalse = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //FileStream with byte order mark detection = false
   StreamReader^ srFromStreamFalse = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}
private void getNewStreamReader()
{
    //Get a new StreamReader in ASCII format from a
    //file using a buffer and byte order mark detection
    StreamReader srAsciiFromFileFalse512 =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //file with byte order mark detection = false
    StreamReader srAsciiFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a file
    StreamReader srAsciiFromFile =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //file with byte order mark detection = false
    StreamReader srFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt", false);
    //Get a new StreamReader from a file
    StreamReader srFromFile =
        new StreamReader("C:\\Temp\\Test.txt");
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false and a buffer
    StreamReader srAsciiFromStreamFalse512 = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false
    StreamReader srAsciiFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a FileStream
    StreamReader srAsciiFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //FileStream with byte order mark detection = false
    StreamReader srFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        false);
    //Get a new StreamReader from a FileStream
    StreamReader srFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
}
Private Sub getNewStreamReader()
    Dim S As Stream = File.OpenRead("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'file using a buffer and byte order mark detection
    Dim SrAsciiFromFileFalse512 As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'file with byte order mark detection = false
    Dim SrAsciiFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a file 
    Dim SrAsciiFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'file with byte order mark detection = false        
    Dim SrFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", False)
    'Get a new StreamReader from a file
    Dim SrFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false and a buffer
    Dim SrAsciiFromStreamFalse512 As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false
    Dim SrAsciiFromStreamFalse = New StreamReader(S, _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a FileStream
    Dim SrAsciiFromStream As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'FileStream with byte order mark detection = false
    Dim SrFromStreamFalse As StreamReader = New StreamReader(S, False)
    'Get a new StreamReader from a FileStream
    Dim SrFromStream As StreamReader = New StreamReader(S)

End Sub

설명

문자 인코딩은 매개 변수에 encoding 의해 설정되고 버퍼 크기는 1024바이트로 설정됩니다. 개체는 StreamReader 스트림의 처음 4바이트에서 인코딩을 검색하려고 시도합니다. 파일이 적절한 바이트 순서 표시로 시작하는 경우 UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32 및 big-endian UTF-32 텍스트를 자동으로 인식합니다. 그렇지 않으면 사용자가 제공한 인코딩이 사용됩니다. 자세한 내용은 Encoding.GetPreamble 메서드를 참조하세요.

개체는 StreamReader 가 호출될 때 StreamReader.Dispose 제공된 개체를 Stream 호출 Dispose() 합니다.

주의

특정 문화권 설정을 사용하여 문자 집합을 컴파일하고 다른 문화권 설정으로 동일한 문자를 검색하는 경우 문자를 해석할 수 없으며 예외가 throw될 수 있습니다.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

추가 정보

적용 대상

StreamReader(Stream, Boolean)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

지정한 바이트 순서 표시 검색 옵션을 사용하여 지정된 스트림에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

public:
 StreamReader(System::IO::Stream ^ stream, bool detectEncodingFromByteOrderMarks);
public StreamReader (System.IO.Stream stream, bool detectEncodingFromByteOrderMarks);
new System.IO.StreamReader : System.IO.Stream * bool -> System.IO.StreamReader
Public Sub New (stream As Stream, detectEncodingFromByteOrderMarks As Boolean)

매개 변수

stream
Stream

읽을 스트림입니다.

detectEncodingFromByteOrderMarks
Boolean

파일의 시작 부분에서 바이트 순서 표시를 찾을지 여부를 나타냅니다.

예외

stream 이 읽기를 지원하지 않습니다.

stream이(가) null인 경우

예제

다음 코드 예제에서는이 StreamReader 생성자를 보여 줍니다.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false,512 );
   
   //Get a new StreamReader in ASCII format from a
   //file with byte order mark detection = false
   StreamReader^ srAsciiFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt" );
   
   //Get a new StreamReader in ASCII format from a
   //FileStream with byte order mark detection = false and a buffer
   StreamReader^ srAsciiFromStreamFalse512 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false,512 );
   
   //Get a new StreamReader in ASCII format from a
   //FileStream with byte order mark detection = false
   StreamReader^ srAsciiFromStreamFalse = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //FileStream with byte order mark detection = false
   StreamReader^ srFromStreamFalse = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}
private void getNewStreamReader()
{
    //Get a new StreamReader in ASCII format from a
    //file using a buffer and byte order mark detection
    StreamReader srAsciiFromFileFalse512 =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //file with byte order mark detection = false
    StreamReader srAsciiFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a file
    StreamReader srAsciiFromFile =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //file with byte order mark detection = false
    StreamReader srFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt", false);
    //Get a new StreamReader from a file
    StreamReader srFromFile =
        new StreamReader("C:\\Temp\\Test.txt");
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false and a buffer
    StreamReader srAsciiFromStreamFalse512 = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false
    StreamReader srAsciiFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a FileStream
    StreamReader srAsciiFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //FileStream with byte order mark detection = false
    StreamReader srFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        false);
    //Get a new StreamReader from a FileStream
    StreamReader srFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
}
Private Sub getNewStreamReader()
    Dim S As Stream = File.OpenRead("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'file using a buffer and byte order mark detection
    Dim SrAsciiFromFileFalse512 As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'file with byte order mark detection = false
    Dim SrAsciiFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a file 
    Dim SrAsciiFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'file with byte order mark detection = false        
    Dim SrFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", False)
    'Get a new StreamReader from a file
    Dim SrFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false and a buffer
    Dim SrAsciiFromStreamFalse512 As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false
    Dim SrAsciiFromStreamFalse = New StreamReader(S, _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a FileStream
    Dim SrAsciiFromStream As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'FileStream with byte order mark detection = false
    Dim SrFromStreamFalse As StreamReader = New StreamReader(S, False)
    'Get a new StreamReader from a FileStream
    Dim SrFromStream As StreamReader = New StreamReader(S)

End Sub

설명

이 생성자는 인코딩을 , BaseStream 매개 변수를 UTF8Encoding사용하는 stream 속성 및 내부 버퍼 크기를 1024바이트로 초기화합니다.

매개 변수는 detectEncodingFromByteOrderMarks 스트림의 처음 4바이트를 확인하여 인코딩을 검색합니다. 파일이 적절한 바이트 순서 표시로 시작하는 경우 UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32 및 big-endian UTF-32 텍스트를 자동으로 인식합니다. 자세한 내용은 Encoding.GetPreamble 메서드를 참조하세요.

개체는 StreamReader 가 호출될 때 StreamReader.Dispose 제공된 개체를 Stream 호출 Dispose() 합니다.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

추가 정보

적용 대상

StreamReader(String)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

지정된 파일 이름에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

public:
 StreamReader(System::String ^ path);
public StreamReader (string path);
new System.IO.StreamReader : string -> System.IO.StreamReader
Public Sub New (path As String)

매개 변수

path
String

읽을 전체 파일 경로입니다.

예외

path가 빈 문자열("")인 경우

path이(가) null인 경우

파일을 찾을 수 없습니다.

지정된 경로가 잘못되었습니다(예: 매핑되지 않은 드라이브에 있음).

path 에 파일 이름, 디렉터리 이름 또는 볼륨 레이블에 대해 올바르지 않거나 잘못된 구문이 포함되어 있습니다.

예제

다음 코드 예제에서는이 StreamReader 생성자를 보여 줍니다.

using namespace System;
using namespace System::IO;
int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   try
   {
      if ( File::Exists( path ) )
      {
         File::Delete( path );
      }
      StreamWriter^ sw = gcnew StreamWriter( path );
      try
      {
         sw->WriteLine( "This" );
         sw->WriteLine( "is some text" );
         sw->WriteLine( "to test" );
         sw->WriteLine( "Reading" );
      }
      finally
      {
         delete sw;
      }

      StreamReader^ sr = gcnew StreamReader( path );
      try
      {
         while ( sr->Peek() >= 0 )
         {
            Console::WriteLine( sr->ReadLine() );
         }
      }
      finally
      {
         delete sr;
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }
}
using System;
using System.IO;

class Test
{
    
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        try
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }

            using (StreamWriter sw = new StreamWriter(path))
            {
                sw.WriteLine("This");
                sw.WriteLine("is some text");
                sw.WriteLine("to test");
                sw.WriteLine("Reading");
            }

            using (StreamReader sr = new StreamReader(path))
            {
                while (sr.Peek() >= 0)
                {
                    Console.WriteLine(sr.ReadLine());
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
Imports System.IO

Public Class Test

    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        Try
            If File.Exists(path) Then
                File.Delete(path)
            End If

            Dim sw As StreamWriter = New StreamWriter(path)
            sw.WriteLine("This")
            sw.WriteLine("is some text")
            sw.WriteLine("to test")
            sw.WriteLine("Reading")
            sw.Close()

            Dim sr As StreamReader = New StreamReader(path)

            Do While sr.Peek() >= 0
                Console.WriteLine(sr.ReadLine())
            Loop
            sr.Close()
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

설명

전체 파일 경로는 매개 변수로 path 지정됩니다. 이 생성자는 인코딩 UTF8Encoding 을 로 초기화하고 버퍼 크기를 1024바이트로 초기화합니다.

매개 변수는 path UNC(유니버설 명명 규칙) 공유의 파일을 포함하여 파일 이름일 수 있습니다.

path 매개 변수는 디스크에 저장된 파일일 필요는 없으며 스트림을 사용하여 액세스를 지원하는 시스템의 일부일 수 있습니다.

주의

특정 문화권 설정을 사용하여 문자 집합을 컴파일하고 다른 문화권 설정으로 동일한 문자를 검색하는 경우 문자를 해석할 수 없으며 예외가 throw될 수 있습니다.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

추가 정보

적용 대상

StreamReader(String, Encoding)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

지정된 문자 인코딩을 사용하여 지정된 파일 이름에 대해 StreamReader 클래스의 새 인스턴스를 초기화합니다.

public:
 StreamReader(System::String ^ path, System::Text::Encoding ^ encoding);
public StreamReader (string path, System.Text.Encoding encoding);
new System.IO.StreamReader : string * System.Text.Encoding -> System.IO.StreamReader
Public Sub New (path As String, encoding As Encoding)

매개 변수

path
String

읽을 전체 파일 경로입니다.

encoding
Encoding

사용할 문자 인코딩입니다.

예외

path가 빈 문자열("")인 경우

path 또는 encodingnull인 경우

파일을 찾을 수 없습니다.

지정된 경로가 잘못되었습니다(예: 매핑되지 않은 드라이브에 있음).

path 에 파일 이름, 디렉터리 이름 또는 볼륨 레이블에 대해 올바르지 않거나 잘못된 구문이 포함되어 있습니다.

예제

다음 코드 예제에서는이 StreamReader 생성자를 보여 줍니다.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false,512 );
   
   //Get a new StreamReader in ASCII format from a
   //file with byte order mark detection = false
   StreamReader^ srAsciiFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt" );
   
   //Get a new StreamReader in ASCII format from a
   //FileStream with byte order mark detection = false and a buffer
   StreamReader^ srAsciiFromStreamFalse512 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false,512 );
   
   //Get a new StreamReader in ASCII format from a
   //FileStream with byte order mark detection = false
   StreamReader^ srAsciiFromStreamFalse = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //FileStream with byte order mark detection = false
   StreamReader^ srFromStreamFalse = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}
private void getNewStreamReader()
{
    //Get a new StreamReader in ASCII format from a
    //file using a buffer and byte order mark detection
    StreamReader srAsciiFromFileFalse512 =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //file with byte order mark detection = false
    StreamReader srAsciiFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a file
    StreamReader srAsciiFromFile =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //file with byte order mark detection = false
    StreamReader srFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt", false);
    //Get a new StreamReader from a file
    StreamReader srFromFile =
        new StreamReader("C:\\Temp\\Test.txt");
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false and a buffer
    StreamReader srAsciiFromStreamFalse512 = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false
    StreamReader srAsciiFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a FileStream
    StreamReader srAsciiFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //FileStream with byte order mark detection = false
    StreamReader srFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        false);
    //Get a new StreamReader from a FileStream
    StreamReader srFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
}
Private Sub getNewStreamReader()
    Dim S As Stream = File.OpenRead("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'file using a buffer and byte order mark detection
    Dim SrAsciiFromFileFalse512 As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'file with byte order mark detection = false
    Dim SrAsciiFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a file 
    Dim SrAsciiFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'file with byte order mark detection = false        
    Dim SrFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", False)
    'Get a new StreamReader from a file
    Dim SrFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false and a buffer
    Dim SrAsciiFromStreamFalse512 As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false
    Dim SrAsciiFromStreamFalse = New StreamReader(S, _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a FileStream
    Dim SrAsciiFromStream As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'FileStream with byte order mark detection = false
    Dim SrFromStreamFalse As StreamReader = New StreamReader(S, False)
    'Get a new StreamReader from a FileStream
    Dim SrFromStream As StreamReader = New StreamReader(S)

End Sub

설명

이 생성자는 매개 변수에 지정된 encoding 대로 인코딩을 초기화하고 내부 버퍼 크기를 1024바이트로 초기화합니다. 개체는 StreamReader 스트림의 처음 4바이트에서 인코딩을 검색하려고 시도합니다. 파일이 적절한 바이트 순서 표시로 시작하는 경우 UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32 및 big-endian UTF-32 텍스트를 자동으로 인식합니다. 그렇지 않으면 사용자가 제공한 인코딩이 사용됩니다. 자세한 내용은 Encoding.GetPreamble 메서드를 참조하세요.

매개 변수는 path UNC(유니버설 명명 규칙) 공유의 파일을 포함하여 파일 이름일 수 있습니다.

path 매개 변수는 디스크에 저장된 파일일 필요는 없으며 스트림을 사용하여 액세스를 지원하는 시스템의 일부일 수 있습니다.

주의

특정 문화권 설정을 사용하여 문자 집합을 컴파일하고 다른 문화권 설정으로 동일한 문자를 검색하는 경우 문자를 해석할 수 없으며 예외가 throw될 수 있습니다.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

추가 정보

적용 대상