2009-06-14 128 views
1

我有以下數據的文本文件:讀取文件到一個數組

計算濃度
30.55 73.48 298.25 27.39 40.98 11.21 99.22 33.46 73.99 12.18 30.7 50 28.4 34.33 29.55 70.48 43.09 28.54 50.78 9.68 62.03 63.18 28.4 100 23.83 68.65 10.93 ?????? 31.42 8.16 24.97 8.3 114.97 34.92 15.53 200 32.15 29.98 23.69 23.41 33.6 92.03 32.73 13.58 58.44 94.61 400 159.98 18.05 50.94 37.12 15.25 46.75 315.22 69.98 13.58 58.77 208.82 11.07 38.15 86.31 35.5 41.88 28.25 5.39 40.83 29.98 54.42 69.48 36.09 13.16 23.26 19.31 147.56 31.86 6.77 19.45 33.6 32.87 205.47 134.21 17.35 9.96 58.61 13.44 23.97 22.13 145.17 29.55 26.54 37.12 198.33

我想將這些數據加載到數組中。

  1. 如何讓用戶打開他在vb.net中選擇的文件?
  2. 如何將這些值讀入vb.net中的數組?

我想澄清的是什麼人-B已建議作品非常非常好。特別是fields = calculationText.split(「」)正是我所需要的;然而,我的下一個問題是以下。上述數據實際上是在此格式:

http://pastebin.com/d29ae565b

其中我需要的第一個值是0,則50,然後100等,並且我需要它讀取列從左至右。問題是這些值沒有以這種方式讀入數組中。這些值實際上是這樣讀的:6.65,84.22,????,35.15。請幫忙!

回答

4

要將文件讀入String變量,在VB.NET中,可以使用System.IO.File.ReadAllText()函數。一個例子是:

Imports System.IO '// placed at the top of the file' 

'// some code' 

Dim calculationText As String 
calculationText = File.ReadAllText("calculations.txt") '// gets all the text in the file' 

其中"calculations.txt"是文件名。

到下一點 - 要允許用戶加載他們選擇的文件,可以使用OpenFileDialog類型。舉個例子:

Dim fileName As String 
Dim openDlg As OpenFileDialog 
openDlg = New OpenFileDialog() '// make a new dialog' 
If openDlg.ShowDialog() = DialogResult.OK Then 
    '// the user clicked OK' 
    fileName = openDlg.FileName '// openDlg.FileName is where it keeps the selected name' 
End If 

結合這一點,我們得到:

Imports System.IO 

'// other code in the file' 

Dim fileName As String 
Dim openDlg As OpenFileDialog 
openDlg = New OpenFileDialog() '// make a new dialog' 
If openDlg.ShowDialog() = DialogResult.OK Then 
    '// the user clicked OK' 
    fileName = openDlg.FileName 
End If 

Dim calculationText As String 
calculationText = File.ReadAllText(fileName) 

現在,我們需要處理的輸入。首先,我們需要列出十進制數字。接下來,我們把一切都在文件中一些在列表中:

Dim numbers As List(Of Decimal) 
numbers = New List(Of Decimal)() '// make a new list' 

Dim fields() As String 
fields = calculationText.Split(" ") '// split all the text by a space.' 

For Each field As String in fields '// whats inside here gets run for every thing in fields' 
    Dim thisNumber As Decimal 
    If Decimal.TryParse(field, thisNumber) Then '// if it is a number' 
     numbers.Add(thisNumber) '// then put it into the list' 
    End If 
Next 

這將不包括在列表中Calculated Concentrations?????。對於可用代碼,只需組合第二個和最後一個代碼示例。

編輯:迴應下面的評論。
隨着List對象,你可以索引他們像這樣:

Dim myNumber As Decimal 
myNumber = numbers(1) 

你並不需要使用Item屬性。此外,它顯示在消息框中的時候,你需要把它變成一個String型第一,是這樣的:

MsgBox(myNumber.ToString()) 
+0

@人-B哎你的代碼不能正常工作,它不是解析 – 2009-06-14 19:23:17

2
  1. OpenFileDialog
  2. 通過文字閱讀的文字,尋找空間來標記號的末尾,然後解析數量將它添加到列表中。或者如果文件總是很短,可以使用StreamReader.ReadToEnd和String.Split。

代碼下手(從C#自動轉換):

Dim ofd = New OpenFileDialog() 
ofd.Title = "Select Data File" 

If ofd.ShowDialog() = DialogResult.OK Then 
    Dim data As New StreamReader(ofd.FileName.ToString()) 
    While data.Read() <> " "c 


    End While 
    ' Read past Calculated 
    While data.Read() <> " "c 


    End While 
    ' Read past Concentrations 
    Dim concentBuilder As New StringBuilder() 
    Dim last As Integer 

    Dim concentrations As New List(Of Double)() 
    Do 
     last = data.Read() 
     If last = " "c OrElse last = -1 Then 
      Dim concentStr As String = concentBuilder.ToString() 
      concentBuilder.Remove(0, concentBuilder.Length) 

      Dim lastConcentration As Double 
      Dim parseSuccess As Boolean = [Double].TryParse(concentStr, lastConcentration) 
      If Not parseSuccess Then 
       Console.[Error].WriteLine("Failed to parse: {0}", concentStr) 
      Else 
       concentrations.Add(lastConcentration) 
      End If 
     Else 
      concentBuilder.Append(CChar(last)) 
     End If 
    Loop While last <> -1 

    For Each d As Double In concentrations 
     Console.WriteLine(d) 
    Next 
End If 
2

一旦你從一個OpenFileDialog控件或其他UI控件來的文件。您可以執行以下操作:

Dim filePath As String = "file.txt" ''* file coming from control 
Dim fileContents As String = System.IO.File.ReadAllText(filePath) 
Dim contentArray() As String = fileContents.Split(" ") 

然後,您可以根據需要遍歷數組和TryParse。