2013-06-04 52 views
0

我有一个二进制文件“blahblah.dat”,它由存储在16byte结构中的数据组成。 我的文件中的每一行都是16个字节长。将二进制文件逐行读入文本框

我想逐行读取此文件,将二进制数据转换为十进制数据,然后将其显示在文本框中。

我已经搜索过,似乎找不到任何合适的东西。


使用此代码:

Using reader As New BinaryReader(File.Open("C:\Users\User\Desktop\test.dat", FileMode.Open)) 
     ' Loop through length of file. 
     Dim pos As Integer = 0 
     Dim length As Integer = reader.BaseStream.Length 
     While pos < 1024 
      ' Read the integer. 
      For i = 0 To 15 
       Dim value As Decimal = reader.ReadByte 
       ' Write to screen. 
       Console.WriteLine(value) 
       TextBox1.Text = TextBox1.Text & value & "," 
       pos += 1 
      Next 
      ' Add length of decimal in bytes to position. 
      TextBox1.Text = TextBox1.Text & vbCrLf 
     End While 
    End Using 

我得到这些结果(这是什么,我认为是正确的 - 我会确认以后):

0,0,128,32,0,0,0,0,2,162,36,18,7,8,3,11, 
0,3,128,32,0,0,0,0,1,138,37,18,7,8,3,11, 
0,51,128,40,0,0,0,0,0,113,38,18,7,8,3,11, 
0,51,128,40,0,0,0,0,3,66,38,18,7,8,3,11, 
0,51,128,40,0,0,0,0,2,42,39,18,7,8,3,11, 
0,51,130,40,0,0,0,0,1,18,40,18,7,8,3,11, 
0,51,130,40,0,0,0,0,3,226,40,18,7,8,3,11, 
0,51,130,40,0,0,0,0,2,202,41,18,7,8,3,11, 
0,51,130,40,0,0,0,0,1,178,42,18,7,8,3,11, 
0,51,130,40,0,0,0,0,0,154,43,18,7,8,3,11, 
0,51,130,40,0,0,0,0,3,106,43,18,7,8,3,11, 
0,51,130,40,0,0,0,0,2,82,44,18,7,8,3,11, 
0,51,130,40,0,0,0,0,1,58,45,18,7,8,3,11, 
0,51,130,40,0,0,0,0,0,34,46,18,7,8,3,11, 
0,51,130,40,0,0,0,0,2,242,46,18,7,8,3,11, 
0,51,130,40,0,0,0,0,1,218,47,18,7,8,3,11, 
0,51,130,40,0,0,0,0,0,195,48,18,7,8,3,11, 
0,51,130,40,0,0,0,0,3,146,48,18,7,8,3,11, 
0,51,130,40,0,0,0,0,2,122,49,18,7,8,3,11, 
0,51,130,40,0,0,0,0,1,98,50,18,7,8,3,11, 
0,51,130,40,0,0,0,0,0,74,51,18,7,8,3,11, 
0,51,130,40,0,0,0,0,3,26,51,18,7,8,3,11, 
0,51,130,40,0,0,0,0,2,3,52,18,7,8,3,11, 
0,51,130,40,0,0,0,0,0,234,53,18,7,8,3,11, 
0,51,130,40,0,0,0,0,3,187,53,18,7,8,3,11, 
0,51,130,40,0,0,0,0,2,163,54,18,7,8,3,11, 
0,51,130,40,0,0,0,0,1,139,55,18,7,8,3,11, 
0,51,130,40,0,0,0,0,0,115,56,18,7,8,3,11, 
0,51,130,40,0,0,0,0,3,67,56,18,7,8,3,11, 
0,51,130,40,0,0,0,0,2,43,57,18,7,8,3,11, 
0,51,130,40,0,0,0,0,1,19,58,18,7,8,3,11, 
0,51,130,40,0,0,0,0,3,227,58,18,7,8,3,11, 
0,51,130,40,0,0,0,0,2,203,59,18,7,8,3,11, 
0,51,130,40,0,0,0,0,1,179,0,19,7,8,3,11, 
0,51,130,40,0,0,0,0,0,156,1,19,7,8,3,11, 
0,51,130,40,0,0,0,0,3,108,1,19,7,8,3,11, 
0,51,130,40,0,0,0,0,2,84,2,19,7,8,3,11, 
0,0,128,32,0,0,0,0,3,172,9,19,7,8,3,11, 
15,0,128,32,0,0,0,0,1,238,10,19,7,8,3,11, 
15,0,128,0,0,0,0,0,0,44,11,19,7,8,3,11, 
15,51,128,8,0,0,0,0,2,80,11,19,7,8,3,11, 
15,51,128,8,0,0,0,0,0,151,12,19,7,8,3,11, 
15,51,128,8,0,0,0,0,2,197,12,19,7,8,3,11, 
15,51,128,8,0,0,0,0,1,10,13,19,7,8,3,11, 
15,51,128,8,0,0,0,0,3,57,13,19,7,8,3,11, 
15,51,128,8,0,0,0,0,1,140,14,19,7,8,3,11, 
15,51,128,8,0,0,0,0,3,192,14,19,7,8,3,11, 
15,51,128,8,0,0,0,0,2,13,15,19,7,8,3,11, 
15,51,128,8,0,0,0,0,0,95,16,19,7,8,3,11, 
15,51,128,8,0,0,0,0,2,145,16,19,7,8,3,11, 
15,51,128,8,0,0,0,0,0,225,17,19,7,8,3,11, 
15,51,128,8,0,0,0,0,3,26,17,19,7,8,3,11, 
15,51,128,8,0,0,0,0,1,104,18,19,7,8,3,11, 
15,51,128,8,0,0,0,0,3,152,18,19,7,8,3,11, 
15,51,128,8,0,0,0,0,1,229,19,19,7,8,3,11, 
15,51,128,8,0,0,0,0,0,44,20,19,7,8,3,11, 
15,51,128,8,0,0,0,0,2,88,20,19,7,8,3,11, 
15,51,128,8,0,0,0,0,0,159,21,19,7,8,3,11, 
15,51,128,8,0,0,0,0,2,213,21,19,7,8,3,11, 
15,51,128,8,0,0,0,0,1,30,22,19,7,8,3,11, 
15,51,128,8,0,0,0,0,3,75,22,19,7,8,3,11, 
15,51,128,8,0,0,0,0,1,145,23,19,7,8,3,11, 
15,51,128,8,0,0,0,0,3,193,23,19,7,8,3,11, 
15,51,128,8,0,0,0,0,2,12,24,19,7,8,3,11, 

会是这样的获取数据的最佳方式还是有办法整理代码?

回答

1

试试这个

Imports System.IO 

Module Module1 
    Sub Main() 
    ' Create the reader in a Using statement. 
    ' ... Use File.Open to open the existing binary file. 
    Using reader As New BinaryReader(File.Open("blahblah.dat", FileMode.Open)) 
     ' Loop through length of file. 
     Dim pos As Integer = 0 
     Dim length As Integer = reader.BaseStream.Length 
     While pos < length 
     ' Read the integer. 
     Dim value As Decimal = reader.ReadDecimal() 
     ' Write to screen. 
     Console.WriteLine(value) 
     ' Add length of decimal in bytes to position. 
     pos += 16 
     End While 
    End Using 
    End Sub 
End Module 
+0

对不起,我忘了说,我在windows窗体创建这个,所以我想有,当我按下一个按钮的事件发生。还有什么方法可以一行一行读取它,而不是一次读取,因为该文件是大约。 3.5mb –

+0

所以从事件中调用此方法 – varun

+0

感谢您的支持。我没有得到我期望的输出。有没有办法从文件中读取原始数据?它将整行读为十进制(这就是我所要求的),但是如果可能的话,我需要将它作为字符串读取,因为我需要将数据分开。 –