다음을 통해 공유


스트림 작성

백업 저장소는 디스크나 메모리 같은 저장 미디어입니다. 각각의 백업 저장소는 Stream 클래스를 구현하는 방식으로 자신의 고유 스트림을 구현합니다. 각 스트림 형식은 지정된 백업 저장소에서 바이트를 읽고 이 저장소에 바이트를 씁니다. 백업 저장소에 연결되는 스트림을 기본 스트림이라고 합니다. 기본 스트림은 백업 저장소에 스트림을 연결하는 데 필요한 매개 변수를 사용하는 생성자를 가집니다. 예를 들어, FileStream은 프로세스에서 파일을 공유하는 방법 등을 지정하는 경로 매개 변수를 지정하는 생성자를 가집니다.

System.IO 클래스 디자인을 사용하여 간소화된 스트림을 작성할 수 있습니다. 원하는 기능을 제공하는 하나 이상의 통과 스트림에 기본 스트림을 추가할 수 있으며 원하는 형식을 손쉽게 읽거나 쓸 수 있도록 체인의 끝에 판독기 또는 작성기를 추가할 수 있습니다.

다음 코드 예제에서는 MyFile.txt를 버퍼링하기 위해 기존의 MyFile.txt에 FileStream을 만듭니다. FileStreams는 기본적으로 버퍼링됩니다. 그런 다음 StreamReader를 만들어 StreamReader에 생성자 인수로 전달되는 FileStream에서 문자를 읽습니다. ReadLinePeek가 더 이상 문자를 찾을 수 없을 때까지 읽습니다.

Imports System
Imports System.IO

Public Class CompBuf
    Private Const FILE_NAME As String = "MyFile.txt"

    Public Shared Sub Main()
        If Not File.Exists(FILE_NAME) Then
            Console.WriteLine("{0} does not exist!", FILE_NAME)
            Return
        End If
        Dim fsIn As new FileStream(FILE_NAME, FileMode.Open, _
            FileAccess.Read, FileShare.Read)
        ' Create an instance of StreamReader that can read
        ' characters from the FileStream.
        Using sr As New StreamReader(fsIn)
            Dim input As String
            ' While not at the end of the file, read lines from the file.
            While sr.Peek() > -1
                input = sr.ReadLine()
                Console.WriteLine(input)
            End While
        End Using
    End Sub
End Class
using System;
using System.IO;

public class CompBuf
{
    private const string FILE_NAME = "MyFile.txt";

    public static void Main()
    {
        if (!File.Exists(FILE_NAME))
        {
            Console.WriteLine("{0} does not exist!", FILE_NAME);
            return;
        }
        FileStream fsIn = new FileStream(FILE_NAME, FileMode.Open,
            FileAccess.Read, FileShare.Read);
        // Create an instance of StreamReader that can read
        // characters from the FileStream.
        using (StreamReader sr = new StreamReader(fsIn))
        {
            string input;
            // While not at the end of the file, read lines from the file.
            while (sr.Peek() > -1)
            {
                input = sr.ReadLine();
                Console.WriteLine(input);
            }
        }
    }
}
using namespace System;
using namespace System::IO;

public ref class CompBuf
{
private:
    static String^ FILE_NAME = "MyFile.txt";

public:
    static void Main()
    {
        if (!File::Exists(FILE_NAME))
        {
            Console::WriteLine("{0} does not exist!", FILE_NAME);
            return;
        }
        FileStream^ fsIn = gcnew FileStream(FILE_NAME, FileMode::Open,
            FileAccess::Read, FileShare::Read);
        // Create an instance of StreamReader that can read
        // characters from the FileStream.
        StreamReader^ sr = gcnew StreamReader(fsIn);
        String^ input;

        // While not at the end of the file, read lines from the file.
        while (sr->Peek() > -1)
        {
            input = sr->ReadLine();
            Console::WriteLine(input);
        }
        sr->Close();
    }
};

int main()
{
    CompBuf::Main();
}

다음 코드 예제에서는 MyFile.txt를 버퍼링하기 위해 기존의 MyFile.txt에 FileStream을 만듭니다. FileStreams는 기본적으로 버퍼링됩니다. 그런 다음 BinaryReader를 만들어 BinaryReader에 생성자 인수로 전달되는 FileStream에서 바이트를 읽습니다. ReadBytePeekChar가 바이트를 더 이상 찾을 수 없을 때까지 읽습니다.

Imports System
Imports System.IO

Public Class ReadBuf
    Private Const FILE_NAME As String = "MyFile.txt"

    Public Shared Sub Main()
        If Not File.Exists(FILE_NAME) Then
            Console.WriteLine("{0} does not exist.", FILE_NAME)
            Return
        End If
        Dim f As New FileStream(FILE_NAME, FileMode.Open, _
            FileAccess.Read, FileShare.Read)
        ' Create an instance of BinaryReader that can
        ' read bytes from the FileStream.
        Using br As new BinaryReader(f)
            Dim input As Byte
            ' While not at the end of the file, read lines from the file.
            While br.PeekChar() > -1
                input = br.ReadByte()
                Console.WriteLine (input)
            End While
        End Using
    End Sub
End Class
using System;
using System.IO;

public class ReadBuf
{
    private const string FILE_NAME = "MyFile.txt";

    public static void Main()
    {
        if (!File.Exists(FILE_NAME))
        {
            Console.WriteLine("{0} does not exist.", FILE_NAME);
            return;
        }
        FileStream f = new FileStream(FILE_NAME, FileMode.Open,
            FileAccess.Read, FileShare.Read);
        // Create an instance of BinaryReader that can
        // read bytes from the FileStream.
        using (BinaryReader br = new BinaryReader(f))
        {
            byte input;
            // While not at the end of the file, read lines from the file.
            while (br.PeekChar() > -1 )
            {
                input = br.ReadByte();
                Console.WriteLine(input);
            }
        }
    }
}
using namespace System;
using namespace System::IO;

public ref class ReadBuf
{
private:
    static String^ FILE_NAME = "MyFile.txt";

public:
    static void Main()
    {
        if (!File::Exists(FILE_NAME))
        {
            Console::WriteLine("{0} does not exist.", FILE_NAME);
            return;
        }
        FileStream^ f = gcnew FileStream(FILE_NAME, FileMode::Open,
            FileAccess::Read, FileShare::Read);
        // Create an instance of BinaryReader that can
        // read bytes from the FileStream.
        BinaryReader^ br = gcnew BinaryReader(f);
        Byte input;
        // While not at the end of the file, read lines from the file.
        while (br->PeekChar() >-1 )
        {
            input = br->ReadByte();
            Console::WriteLine(input);
        }
        br->Close();
    }
};

int main()
{
    ReadBuf::Main();
}

참고 항목

참조

StreamReader

StreamReader.ReadLine

StreamReader.Peek

FileStream

BinaryReader

BinaryReader.ReadByte

BinaryReader.PeekChar

개념

기본 파일 I/O

작성기 만들기