Instrukcje: odczytywanie i zapisywanie w nowo utworzonym pliku danych

Klasy i System.IO.BinaryReader służą do zapisywania System.IO.BinaryWriter i odczytywania danych innych niż ciągi znaków. W poniższym przykładzie pokazano, jak utworzyć pusty strumień plików, zapisać w nim dane i odczytać z niego dane.

W przykładzie tworzony jest plik danych o nazwie Test.data w bieżącym katalogu, tworzy skojarzone obiekty BinaryWriter i BinaryReader używa BinaryWriter obiektu do zapisania liczb całkowitych od 0 do 10 do pliku Test.data, co pozostawia wskaźnik pliku na końcu pliku. Następnie BinaryReader obiekt ustawia wskaźnik pliku z powrotem na źródło i odczytuje określoną zawartość.

Uwaga

Jeśli plik Test.data już istnieje w bieżącym katalogu, zgłaszany IOException jest wyjątek. Użyj opcji FileMode.Create tryb plików, a nie FileMode.CreateNew zawsze, aby utworzyć nowy plik bez zgłaszania wyjątku.

Przykład

using System;
using System.IO;

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

    public static void Main()
    {
        if (File.Exists(FILE_NAME))
        {
            Console.WriteLine($"{FILE_NAME} already exists!");
            return;
        }

        using (FileStream fs = new FileStream(FILE_NAME, FileMode.CreateNew))
        {
            using (BinaryWriter w = new BinaryWriter(fs))
            {
                for (int i = 0; i < 11; i++)
                {
                    w.Write(i);
                }
            }
        }

        using (FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read))
        {
            using (BinaryReader r = new BinaryReader(fs))
            {
                for (int i = 0; i < 11; i++)
                {
                    Console.WriteLine(r.ReadInt32());
                }
            }
        }
    }
}


// The example creates a file named "Test.data" and writes the integers 0 through 10 to it in binary format.
// It then writes the contents of Test.data to the console with each integer on a separate line.
Imports System.IO

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

    Public Shared Sub Main()
        If File.Exists(FILE_NAME) Then
            Console.WriteLine($"{FILE_NAME} already exists!")
            Return
        End If

        Using fs As New FileStream(FILE_NAME, FileMode.CreateNew)
            Using w As New BinaryWriter(fs)
                For i As Integer = 0 To 10
                    w.Write(i)
                Next
            End Using
        End Using

        Using fs As New FileStream(FILE_NAME, FileMode.Open, FileAccess.Read)
            Using r As New BinaryReader(fs)
                For i As Integer = 0 To 10
                    Console.WriteLine(r.ReadInt32())
                Next
            End Using
        End Using
    End Sub
End Class

' The example creates a file named "Test.data" and writes the integers 0 through 10 to it in binary format.
' It then writes the contents of Test.data to the console with each integer on a separate line.

Zobacz też