如何读取和写入多个二进制值 [英] How to Read and Write multiple Binary values

查看:76
本文介绍了如何读取和写入多个二进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,其中用户在文本框中输入数据,然后单击保存",该程序将其以二进制格式保存在文件中.这是它的代码:

I have a program in which the data is entered by the user in text-boxes and then on clicking save the program saves it in a binary format in a file. Here is the code for it :

Public Sub BinaryExport()
       Dim export As FileStream
       Try
           export = New FileStream(Application.StartupPath + "\read.dat", FileMode.Create)
       Catch e As Exception
           MsgBox(e.Message)
       End Try

       Dim binExport As BinaryWriter = New BinaryWriter(export)

       Try
           binExport.Write(TextBox1.Text)
           binExport.Write(TextBox2.Text)
           binExport.Write(TextBox3.Text)
           binExport.Close()
           MsgBox("Saved SuccessFully")
       Catch e1 As Exception
           MsgBox(e1.Message)
       End Try

   End Sub



如何存储多个数据输入并使用vb.net读取它们

我使用以下代码进行阅读,但失败了



How do I store multiple data inputs and read them using vb.net

I used the following code for reading but failed

Public Sub BinaryImport()
        Dim import As FileStream
        Try
            import = New FileStream(Application.StartupPath + "\read.dat", FileMode.Open)
        Catch e2 As Exception
            MsgBox("File NOT FOUND")
        End Try

        Dim BinaryReader As BinaryReader = New BinaryReader(import)

        Try
            TextBox1.Text = BinaryReader.ReadString
            TextBox2.Text = BinaryReader.ReadString
            TextBox3.Text = BinaryReader.ReadString
            BinaryReader.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub


请帮忙!


Please Help!

推荐答案

要将更多数据添加到文件中,您应该更改以下行:
To add more data to your file you should change this line:
export = New FileStream(Application.StartupPath + "\read.dat", FileMode.Create)


至:


to:

export = New FileStream(Application.StartupPath + "\read.dat", FileMode.Append)



如果文件不存在,则会创建该文件,否则将其打开,并将写指针定位在末尾,以准备添加新数据.

希望这会有所帮助.



This will create the file if it does not exist, otherwise it opens it and positions the write pointer at the end ready to add new data.

Hope this helps.


这篇关于如何读取和写入多个二进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆