방법: 새로 만든 데이터 파일 읽기 및 쓰기

BinaryWriterBinaryReader 클래스는 문자열이 아닌 데이터를 쓰고 읽는 데 사용됩니다. 다음 코드 예제에서는 새로 만든 빈 파일 스트림(Test.data)에서 데이터를 읽고 이 스트림에 데이터를 쓰는 경우를 보여 줍니다. 현재 디렉터리에 데이터 파일을 만든 후 관련된 BinaryWriterBinaryReader를 만들고 BinaryWriter를 사용하여 0부터 10까지의 정수를 Test.data에 씁니다. 이렇게 하면 파일 포인터가 파일 끝에 옵니다. BinaryReader는 파일 포인터를 다시 원점으로 설정한 후 지정된 내용을 읽습니다.

예제

Imports System
Imports System.IO

Class MyStream
    Private Const FILE_NAME As String = "Test.data"

    Public Shared Sub Main()
        ' Create the new, empty data file.
        If File.Exists(FILE_NAME) Then
            Console.WriteLine("{0} already exists!", FILE_NAME)
            Return
        End If
        Using fs As New FileStream(FILE_NAME, FileMode.CreateNew)
            ' Create the writer for data.
            Using w As New BinaryWriter(fs)
                ' Write data to Test.data.
                For i As Integer = 0 To 10
                    w.Write(i)
                Next
            End Using
        End Using
        ' Create the reader for data.
        Using fs As New FileStream(FILE_NAME, FileMode.Open, FileAccess.Read)
            Using r As New BinaryReader(fs)
                ' Read data from Test.data.
                For i As Integer = 0 To 10
                    Console.WriteLine(r.ReadInt32())
                Next
            End Using
        End Using
    End Sub
End Class
using System;
using System.IO;

class MyStream
{
    private const string FILE_NAME = "Test.data";

    public static void Main()
    {
        // Create the new, empty data file.
        if (File.Exists(FILE_NAME))
        {
            Console.WriteLine("{0} already exists!", FILE_NAME);
            return;
        }
        using (FileStream fs = new FileStream(FILE_NAME, FileMode.CreateNew))
        {
            // Create the writer for data.
            using (BinaryWriter w = new BinaryWriter(fs))
            {
                // Write data to Test.data.
                for (int i = 0; i < 11; i++)
                {
                    w.Write(i);
                }
            }
        }
        // Create the reader for data.
        using (FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read))
        {
            using (BinaryReader r = new BinaryReader(fs))
            {
                // Read data from Test.data.
                for (int i = 0; i < 11; i++)
                {
                    Console.WriteLine(r.ReadInt32());
                }
            }
        }
    }
}
using namespace System;
using namespace System::IO;

ref class MyStream
{
private:
    static String^ FILE_NAME = "Test.data";

public:
    static void Main()
    {
        // Create the new, empty data file.
        if (File::Exists(FILE_NAME))
        {
            Console::WriteLine("{0} already exists!", FILE_NAME);
            return;
        }
        FileStream^ fs = gcnew FileStream(FILE_NAME, FileMode::CreateNew);
        // Create the writer for data.
        BinaryWriter^ w = gcnew BinaryWriter(fs);
        // Write data to Test.data.
        for (int i = 0; i < 11; i++)
        {
            w->Write(i);
        }
        w->Close();
        fs->Close();
        // Create the reader for data.
        fs = gcnew FileStream(FILE_NAME, FileMode::Open, FileAccess::Read);
        BinaryReader^ r = gcnew BinaryReader(fs);
        // Read data from Test.data.
        for (int i = 0; i < 11; i++)
        {
            Console::WriteLine(r->ReadInt32());
        }
        fs->Close();
    }
};

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

강력한 프로그래밍

현재 디렉터리에 Test.data가 이미 있는 경우 IOException이 throw됩니다. IOException을 throw하지 않고 항상 새 파일을 만들려면 FileMode.Create를 사용합니다.

참고 항목

작업

방법: 디렉터리 목록 만들기

방법: 로그 파일 열기 및 추가

방법: 파일의 텍스트 읽기

방법: 파일에 텍스트 쓰기

방법: 문자열에서 문자 읽기

방법: 문자열에 문자 쓰기

참조

BinaryReader

BinaryWriter

FileStream

FileStream.Seek

SeekOrigin

개념

기본 파일 I/O