Aracılığıyla paylaş


Nasıl yapılır: Yeni oluşturulan bir veri dosyasını okuma ve bu dosyaya yazma

System.IO.BinaryWriter ve System.IO.BinaryReader sınıfları, karakter dizeleri dışında veri yazmak ve okumak için kullanılır. Aşağıdaki örnekte boş bir dosya akışı oluşturma, akışa veri yazma ve bu akıştan veri okuma adımları gösterilmektedir.

Örnek, geçerli dizinde Test.data adlı bir veri dosyası oluşturur, ilişkili BinaryWriter ve BinaryReader nesneleri oluşturur ve 0 ile 10 arasında tamsayıları Test.data dosyasına yazmak için nesnesini kullanır BinaryWriter ve bu da dosya işaretçisini dosyanın sonunda bırakır. Nesne BinaryReader daha sonra dosya işaretçisini kaynağına geri ayarlar ve belirtilen içeriği okur.

Not

Geçerli dizinde Test.data zaten varsa, bir IOException özel durum oluşturulur. Özel durum oluşturmadan her zaman yeni bir dosya oluşturmak yerine FileMode.CreateNew dosya modu seçeneğini FileMode.Create kullanın.

Örnek

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.

Ayrıca bkz.