2013-06-04 77 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

感謝您的支持。我沒有得到我期望的輸出。有沒有辦法從文件中讀取原始數據?它將整行讀爲十進制(這就是我所要求的),但是如果可能的話,我需要將它作爲字符串讀取,因爲我需要將數據分開。 –