VB.NET - 从文本文件加载值列表

时间:2009-10-20 04:28:35

标签: vb.net

我有一个类似如下的文本文件:

[组1] 值1 值2 值3

[第2组] 值1 值2

[第3组] 值3 价值4

我希望能够做什么,是根据传入的组值将值加载到数组(或列表?)中。例如。如果我传入“group2”,那么它将返回“value1”和“value2”的列表。

这些值通常也不会改变(可能每6个月左右),那么是否有更好的方法来存储它们而不是普通的旧文本文件,以便加快加载速度等等?

感谢您的帮助。

Leddo

3 个答案:

答案 0 :(得分:2)

这是一个家庭工作问题?

使用StreamReader类读取文件(您可能需要使用.EndOfStream和ReadLine())并使用String类进行字符串操作(可能是.StartsWith(),. Substring()和.Split()

至于存储“IT DEPENDS”的更好方法。你将拥有多少组,有多少值,访问数据的频率等等。问题的原始措辞可能会让我们更好地了解听到后的内容。

<强>增加:

所以,假设这个程序/服务一整天都在运行,并且文件不是很大,那么你可能只想将文件读入一个Dictionary(String,List(of String)) 。 ContainsKey方法将确定一个组是否存在。

   Function GetValueSet(ByVal filename As String) As Dictionary(Of String, List(Of String))
        Dim valueSet = New Dictionary(Of String, List(Of String))()
        Dim lines = System.IO.File.ReadAllLines(filename)
        Dim header As String
        Dim values As List(Of String) = Nothing
        For Each line As String In lines
            If line.StartsWith("[") Then
                If Not values Is Nothing Then
                    valueSet.add(header, values)
                End If
                header = GetHeader(line)
                values = New List(Of String)()
            ElseIf Not values Is Nothing Then
                Dim value As String = line.Trim()
                If value <> "" Then
                    values.Add(value)
                End If
            End If
        Next
        If Not values Is Nothing Then
            valueSet.add(header, values)
        End If
        Return valueSet
    End Function

    Function GetHeader(ByVal line As String)
        Dim index As Integer = line.IndexOf("]")
        Return line.Substring(1, index - 1)
    End Function

<强>增加:

现在,如果您运行多线程解决方案(包括所有ASP.Net解决方案),那么您要么确保在应用程序启动时执行此操作(对于Global.asax中的ASP.Net,我认为它是ApplicationStart或OnStart或其他东西),或者你需要锁定。默认情况下,WinForms和Services不是多线程的。

此外,如果文件发生更改,您需要重新启动app / service / web-site,或者您需要添加文件监视器来重新加载数据(然后多线程需要锁定,因为这不再局限于应用程序启动)。

答案 1 :(得分:1)

好吧,这就是我最终编码的内容:

   Public Function FillFromFile(ByVal vFileName As String, ByVal vGroupName As String) As List(Of String)
        ' open the file
        ' read the entire file into memory
        ' find the starting group name
        Dim blnFoundHeading As Boolean = False
        Dim lstValues As New List(Of String)

        Dim lines() As String = IO.File.ReadAllLines(vFileName)
        For Each line As String In lines
            If line.ToLower.Contains("[" & vGroupName.ToLower & "]") Then
                ' found the heading, now start loading the lines into the list until the next heading
                blnFoundHeading = True
            ElseIf line.Contains("[") Then
                If blnFoundHeading Then
                    ' we are at the end so exit the loop
                    Exit For
                Else
                    ' its another group so keep going

                End If
            Else
                If blnFoundHeading And line.Trim.Length > 0 Then
                    lstValues.Add(line.Trim)
                End If
            End If
        Next

        Return lstValues

    End Function

答案 2 :(得分:0)

关于存储数据的更好方法:您可能会发现XML很有用。将XML数据读入DataTable对象非常容易。

示例:

Dim dtTest                        As New System.Data.DataTable
dtTest.ReadXml("YourFilePathNameGoesHere.xml")