Share via


如何:对新建的数据文件进行读取和写入

BinaryWriterBinaryReader 类用于读取和写入数据,而不是用于读取和写入字符串。 下面的代码示例演示如何向新的空文件流 (Test.data) 写入数据及从中读取数据。 在当前目录中创建了数据文件之后,也就同时创建了相关的 BinaryWriterBinaryReaderBinaryWriter 用于向 Test.data 写入整数 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。 始终使用 FileMode.Create 创建新文件,而不引发 IOException

请参见

任务

如何:创建目录列表

如何:打开并追加到日志文件

如何:从文件读取文本

如何:向文件写入文本

如何:从字符串中读取字符

如何:向字符串写入字符

参考

BinaryReader

BinaryWriter

FileStream

FileStream.Seek

SeekOrigin

概念

基本的文件 I/O