将文件读入数组

时间:2009-06-14 18:46:22

标签: vb.net

我有一个包含以下数据的文本文件:

计算浓度
    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中将这些值读入数组?

  3. 我想澄清一下b的建议非常好。特别是fields = calculationText.split(“”)正是我所需要的;但是,我的下一个问题如下。上面的数据实际上是这种格式:

    http://pastebin.com/d29ae565b

    其中我需要第一个值为0,然后是50,然后是100等等,我需要它从左到右读取列。问题是这些值没有以这种方式读入数组。这些值实际上是这样读的:6.65,84.22,????,35.15。请帮忙!

3 个答案:

答案 0 :(得分: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())

答案 1 :(得分:2)

  1. OpenFileDialog
  2. 逐个字符读取,查找标记数字末尾的空格,然后解析数字并将其添加到List中。或者,如果文件总是很短,您可以使用StreamReader.ReadToEnd和String.Split。
  3. 开始的代码(从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 :(得分: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到一个数字。