다음을 통해 공유


FileInfo.OpenText 메서드

정의

UTF8 인코딩 방식으로 기존 텍스트 파일에서 읽는 StreamReader 를 만듭니다.

public:
 System::IO::StreamReader ^ OpenText();
public System.IO.StreamReader OpenText ();
member this.OpenText : unit -> System.IO.StreamReader
Public Function OpenText () As StreamReader

반환

UTF8 인코딩 방식을 사용하는 새 StreamReader 입니다.

예외

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

파일을 찾을 수 없습니다.

Name 가 읽기 전용이거나 디렉터리입니다.

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

예제

다음 예제에서는 파일에서 텍스트를 읽습니다.

using namespace System;
using namespace System::IO;
using namespace System::Text;

public ref class OpenTextTest
{
public:
    static void Main()
    {
        String^ path = "c:\\MyTest.txt";

        FileInfo^ fi = gcnew FileInfo(path);

        // Check for existing file
        if (!fi->Exists)
        {
            // Create the file.
            FileStream^ fs = fi->Create();
            array<Byte>^ info =
                (gcnew UTF8Encoding(true))->GetBytes("This is some text in the file.");

            // Add some information to the file.
            fs->Write(info, 0, info->Length);
            fs->Close();
        }

        // Open the stream and read it back.
        StreamReader^ sr = fi->OpenText();
        String^ s = "";
        while ((s = sr->ReadLine()) != nullptr)
        {
            Console::WriteLine(s);
        }
    }
};

int main()
{
    OpenTextTest::Main();
}
//This code produces output similar to the following; 
//results may vary based on the computer/file structure/etc.:
//
//This is some text in the file.
//
using System;
using System.IO;
using System.Text;

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

        FileInfo fi = new FileInfo(path);

        // Check for existing file
        if (!fi.Exists)
        {
            // Create the file.
            using (FileStream fs = fi.Create())
            {
                Byte[] info =
                    new UTF8Encoding(true).GetBytes("This is some text in the file.");

                // Add some information to the file.
                fs.Write(info, 0, info.Length);
                fs.Close();
            }
        }

        // Open the stream and read it back.
        using (StreamReader sr = fi.OpenText())
        {
            string s = "";
            while ((s = sr.ReadLine()) != null)
            {
                Console.WriteLine(s);
            }
        }
    }
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//This is some text in the file.
//
Imports System.IO
Imports System.Text

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

        Dim fi As New FileInfo(path)

        ' Check for existing file
        If fi.Exists = false Then
            ' Create the file.
            Dim fs As FileStream = fi.Create()
            Dim info() As Byte = _
                    New UTF8Encoding(true).GetBytes("This is some text in the file.")

            ' Add some information to the file.
            fs.Write(info, 0, info.Length)
            fs.Close()
        End If

        ' Open the stream and read it back.
        Dim sr As StreamReader = fi.OpenText()
        Dim s As String = ""
        While sr.EndOfStream = false
            s = sr.ReadLine()
            Console.WriteLine(s)
        End While
        sr.Close()
    End Sub
End Class
'This code produces output similar to the following;
'results may vary based on the computer/file structure/etc.:
'
'This is some text in the file.
'

적용 대상

추가 정보