在Visual Basic中存储文本文件中的记录

时间:2013-09-18 22:47:54

标签: arrays vb.net visual-studio-2010 visual-studio sorting

我无法从文本文件中提取记录并将它们分成数组。

所有记录都采用以下格式:

  --------------------------- CITATION   01 OF   99 -------------------------
Authors:  Oliver, Ron
Oliver, Helen
Title:  Information Access and Retrieval with Hypermedia Information Systems. /
 by Oliver, Ron; Oliver, Helen
Pub.Date:  1996
Document no.:  EJ520272
FOUND IN:  British Journal of Educational Technology; v27 n1 p33-44 Jan 1996
Abstract:  Describes an investigation of the search strategies employed by
 novice users of an interactive information system. A class of 12-year-old
 students was instructed in use of an electronic encyclopedia, which they used
 as an information source for projects and to complete skills tests. Students
 employed inefficient search strategies and had difficulty creating search
 requests for information-related problems. (Author/JKP)
Pub.Type:  Research/technical(143); Journal article(080)
Language:  English
RIE/CIJE issue:  CIJJUL96
If not avail. in your library or through ILL, for sale from:  UMI
Minor Identifiers:  Electronic Media
Major Descriptors:  Hypermedia
Information Retrieval
Online Searching
Optical Data Disks
Search Strategies
Minor Descriptors:  Access to Information
Elementary Education
Encyclopedias
Information Sources
Problems
ISSN:  0007-1013
  --------------------------- CITATION   02 OF   99 -------------------------
Authors:  Kimmel, Stacey
Title:  Robot-Generated Databases on the World Wide Web. / by Kimmel, Stacey
Pub.Date:  1996
Document no.:  EJ520165
FOUND IN:  Database; v19 n1 p40-43,46-49 Feb-Mar 1996
Abstract:  Provides an overview of robots that retrieve World Wide Web documents
 and index data and then store it in a database. Nine robot-generated databases
 are described, including record content, services, search features, and sample
 search results; and sidebars discuss the controversy about Web robots and other
 resource discovery tools. (LRW)
Pub.Type:  Descriptive(141); Journal article(080)
Language:  English
RIE/CIJE issue:  CIJJUL96
If not avail. in your library or through ILL, for sale from:  UMI
Major Identifiers:  World Wide Web
Minor Identifiers:  Examples
Major Descriptors:  Databases
Information Retrieval
Online Searching
Robotics
Minor Descriptors:  Comparative Analysis
Indexing
Problems
Search Strategies
ISSN:  0162-4105

And so on....

我只需要以下信息:文本文件中每条记录的作者,标题,FOUND IN和摘要。你到底怎么做呢?

我的代码:

Public Class Form1

    Private Sub btnImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImport.Click

        Dim fd As New OpenFileDialog()
        fd.Title = "Please select a txt file"
        fd.InitialDirectory = Application.StartupPath & "\inc"
        fd.Filter = "Text Documents (*.txt)|*.txt"
        fd.Multiselect = False
        fd.RestoreDirectory = True
        Dim strFileName As String

        If (fd.ShowDialog() = DialogResult.OK) Then
            strFileName = fd.FileName
            Dim sr As System.IO.StreamReader = System.IO.File.OpenText(strFileName)


            Dim recs As String = sr.ReadToEnd
            ''Dim rec() As String = recs.Split(" ")

            sr.Close()

            txtAbstract.Text = recs(0)

        End If
    End Sub

End Class

我正在使用Microsoft Visual Studio 2010 Express。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

不要做sr.ReadToEnd。逐行阅读,这将很容易(更容易)。

Using sr As IO.StreamReader = fi.OpenText
    Do While sr.Peek > -1
        sLine = sr.ReadLine
        'Your code to parse the line or read the data here
    Loop
End Using

答案 1 :(得分:0)

使用LINQ和列表框,您可以获得所需的一切:

ListBox1.DataSource = (From line In IO.File.ReadAllLines(strFileName)
                        Where(line.StartsWith("Authors") OrElse _
                              line.StartsWith("Title") OrElse _
                              line.StartsWith("FOUND IN") OrElse _
                              line.StartsWith("Abstract"))
                       Select line).ToList

这假设数据跨越样本中的多行,实际上它是文件中的一行。

如果实际上它们是单独的行,这样的东西应该起作用:

    Dim lines As New List(Of String)
    Dim continueadd As Boolean = False
    Dim sr As New IO.StreamReader(strFileName)
    While Not sr.EndOfStream
        Dim line = sr.ReadLine
        If line.StartsWith("Authors") OrElse _
            line.StartsWith("Title") OrElse _
            line.StartsWith("FOUND IN") OrElse _
            line.StartsWith("Abstract") Then
            continueadd = True
            lines.Add(line)
        ElseIf Not line.Contains(":  ") AndAlso continueadd Then
            lines.Add(line)
        ElseIf line.Contains(":  ") Then
            continueadd = False
        End If
    End While
    ListBox1.DataSource = lines