HOW TO:讀取和寫入新建立的資料檔案How to: Read and write to a newly created data file

System.IO.BinaryWriterSystem.IO.BinaryReader 類別用於寫入和讀取資料,而不是字元字串。The System.IO.BinaryWriter and System.IO.BinaryReader classes are used for writing and reading data other than character strings. 下列範例顯示如何建立空的檔案資料流、將資料寫入其中,以及從中讀取資料。The following example shows how to create an empty file stream, write data to it, and read data from it.

此範例會在目前的目錄中建立一個稱為 Test.data 的資料檔案、建立相關聯的 BinaryWriterBinaryReader 物件,而且會使用 BinaryWriter 物件,將 0 到 10 的整數寫入 Test.data,讓檔案指標留在檔案的結尾。The example creates a data file called Test.data in the current directory, creates the associated BinaryWriter and BinaryReader objects, and uses the BinaryWriter object to write the integers 0 through 10 to Test.data, which leaves the file pointer at the end of the file. BinaryReader 物件接著會將檔案指標設定為原點, 並讀出指定的內容。The BinaryReader object then sets the file pointer back to the origin and reads out the specified content.

注意

如果 Test.data 已存在於目前的目錄中,則會擲回 IOException 例外狀況。If Test.data already exists in the current directory, an IOException exception is thrown. 使用檔案模式選項 FileMode.Create,而不是 FileMode.CreateNew,來一律建立新檔案,而不會擲回例外狀況。Use the file mode option FileMode.Create rather than FileMode.CreateNew to always create a new file without throwing an exception.

範例Example

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.

另請參閱See also