【问题标题】:Read from binary file and convert byte array into string从二进制文件中读取并将字节数组转换为字符串
【发布时间】:2012-06-24 09:19:51
【问题描述】:

我正在尝试使用以下代码从二进制文件中读取二进制数据,但它返回字节数组中的值。如何从二进制文件中读取二进制数据,然后将数据转换为字符串?

这就是我创建二进制文件的方式。

Dim fs As New FileStream(Application.StartupPath & "\Agency.dat", FileMode.OpenOrCreate)
    Dim bf As New BinaryFormatter()
    Call bf.Serialize(fs, GAgency)
    fs.Close()

读取二进制文件的代码。

Public Shared Function ReadBinaryFile(ByVal path As String)
    ' Open the binary file.
    Dim streamBinary As New FileStream(path, FileMode.Open)

    ' Create a binary stream reader object.
    Dim readerInput As BinaryReader = New BinaryReader(streamBinary)

    ' Determine the number of bytes to read.
    Dim lengthFile As Integer = FileSize(path)

    ' Read the data in a byte array buffer.
    Dim inputData As Byte() = readerInput.ReadBytes(lengthFile)

    ' Close the file.
    streamBinary.Close()
    readerInput.Close()

    Return inputData
End Function 'ReadBinaryData’

【问题讨论】:

    标签: vb.net binary-data


    【解决方案1】:

    试一试

    Dim objGAgency As GAgency
    Dim fs As Stream = New FileStream( Application.StartupPath & "\Agency.dat", FileMode.Open)
    Dim bf As BinaryFormatter = New BinaryFormatter()
    
    objGAgency = CType(bf.Deserialize(fs), GAgency)
    fs.Close()
    

    【讨论】:

    • 你试过反序列化吗?
    猜你喜欢
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 2015-12-16
    相关资源
    最近更新 更多