Share via


스트림 작성

업데이트: 2007년 11월

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

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

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

Option Explicit On 
Option Strict On
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.
        Dim sr As New StreamReader(fsIn)
        ' While not at the end of the file, read lines from the file.
        While sr.Peek() > -1
            Dim input As String = sr.ReadLine()
            Console.WriteLine(input)
        End While
        sr.Close()
    End Sub
End Class
using System;
using System.IO;
public class CompBuf 
{
    private const string FILE_NAME = "MyFile.txt";
    public static void Main(String[] args) 
    {
        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.
        StreamReader sr = new StreamReader(fsIn); 
        // While not at the end of the file, read lines from the file.
        while (sr.Peek()>-1) 
        {
            String input = sr.ReadLine();
            Console.WriteLine (input);
        }
        sr.Close();
    }
}

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

Option Explicit On 
Option Strict On
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.
        Dim sr As New BinaryReader(f)
        ' While not at the end of the file, read lines from the file.
        While sr.PeekChar() > -1
            Dim input As Byte = sr.ReadByte()
            Console.WriteLine(input)
        End While
        sr.Close()
    End Sub
End Class
using System;
using System.IO;
public class ReadBuf 
{
    private const string FILE_NAME = "MyFile.txt";
    public static void Main(String[] args) 
    {
        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.
        BinaryReader sr = new BinaryReader(f); 
        // While not at the end of the file, read lines from the file.
        while (sr.PeekChar()>-1) 
        {
            byte input = sr.ReadByte();
            Console.WriteLine (input);
        }
        sr.Close();
    }
}

참고 항목

개념

기본 파일 I/O

작성기 만들기

참조

StreamReader

StreamReader.ReadLine

StreamReader.Peek

FileStream

BinaryReader

BinaryReader.ReadByte

BinaryReader.PeekChar