Imports System.IO           '引入命名空间
Public Class Form1
    Private Sub BtnCreateBin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCreateBin.Click
        Dim fs As FileStream = New FileStream("E:\project\Exp11-3\myfile.dat", FileMode.Create)
        Dim bw As BinaryWriter = New BinaryWriter(fs)
        Dim i As Integer
        Dim str As String = "This is a string"
        Dim btText() As Byte = {0, 100, 200, 255, 5}
        ' 将12个连续整数写入流
        For i = 0 To 11
            bw.Write(i)
        Next
        bw.Write(str)       '将一个字符串写入流
        bw.Write(btText)    '将字节数组写入流
        bw.Close()
        fs.Close()
        MsgBox("文件创建及写入数据成功！")
        BtnReadBin.Enabled = True
    End Sub

    Private Sub BtnOpenBin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReadBin.Click
        Dim fstream As New FileStream("E:\project\Exp11-3\myfile.dat", FileMode.Open, FileAccess.Read)
        Dim br As BinaryReader
        Dim btText() As Byte
        br = New BinaryReader(fstream)
        TextBox1.Text &= "******12个连续整数（0-11）******" & vbCrLf
        ' 从流中读出12个连续整数并显示在文本框中
        For i = 0 To 11
            TextBox1.Text &= br.ReadInt32() & " "
        Next
        TextBox1.Text &= vbCrLf & "******1个字符串******" & vbCrLf
        ' 从流中读出1个字符串并显示在文本框中
        TextBox1.Text &= br.ReadString & vbCrLf
        TextBox1.Text &= "******1个字节数组******" & vbCrLf
        ' 从流中读出1个字节数组并显示在文本框中
        btText = br.ReadBytes(5)
        For i = 0 To 4
            TextBox1.Text &= btText(i) & " "
        Next
    End Sub
End Class
