【问题标题】:Read complex text file using Vb.net使用 Vb.net 读取复杂的文本文件
【发布时间】:2017-03-14 01:51:31
【问题描述】:

文件结构

RECORD
1234567890,123456789,1234567,Address1,,City,State,Zip,USA,Phone,,,,,,,,,
EmpName,DBAName,ID,,Address1,,Address1,City,60603,USA,234567890,,,,
C,03/13/2017,,1,2,
RECORD
1234567890,123456789,1234567,Address1,,City,State,Zip,USA,Phone,,,,,,,,,
EmpName2,DBAName2,ID2,,Address2,,Address2,City2,60603,USA,234567890,,,,
C,03/13/2017,,1,2,

查看上面的文件结构,我想循环遍历文件,对于每条记录(RECORD),我想将接下来的三行放在一个数组中,然后将下一条记录放在单独的数组中。

我正在寻找骨架代码,我对使用流阅读器进行编程非常陌生。

到目前为止的代码

 Dim read As New System.IO.StreamReader("C:\New Text.txt")
       Dim a As String

       For Each a In read.ReadLine
           If read.ReadLine.Equals("RECORD") Then
               \\How do I read next 3 lines and put them in one array with comma delimiter
           End If
       Next

【问题讨论】:

  • 我会查找string.Split() 函数和File.ReadAllText() 这实际上并不难。即使在此页面本身VB.NET Read Certain text in a text fileRelated 下的第 5 个链接上也有很多工作示例@
  • 就这么做吧。阅读接下来的 3 行(ReadLine),然后连接你得到的 3 个字符串。
  • @pm100 你能告诉我如何只阅读接下来的三行吗?然后如何将它们放入数组中。如果你能写一些代码,我将不胜感激

标签: vb.net file


【解决方案1】:

你需要一个数据结构来保持加载行,然后开始你的循环

' Here you will keep the 3 lines block joined together and splitted on comma
Dim records = new List(Of String())()

Using read As New System.IO.StreamReader("C:\New Text.txt")
   Dim a As String

   ' Read each line (Assuming that we start with a RECORD line
   For Each a In read.ReadLine
       ' Do not read another line, but just test what is present in the string
       If a.Equals("RECORD") Then

          ' Now read the 3 following lines....
          Dim line1 = read.ReadLine
          Dim line2 = read.ReadLine
          Dim line3 = read.ReadLine
          'Join the lines togeter
          Dim record = string.Join("", line1, line2, line3).
          ' Split on the comma to produce an array of strings. The fields.
          Dim fields = record.Split(","c)
          records.Add(fields)
       End If
   Next
End Using

当然,这应该使用适当的类来增强,该类描述您的输入,其中类的每个属性代表加载的 CSV 文件的一个字段。然后您可以将 List(Of String()) 更改为 List(Of YourClassData)

请记住,此解决方案非常依赖于确切的文件结构。带有“RECORD”内容的一行后面应该总是跟着三行数据。 (三行数据之间不能有空行)

【讨论】:

    【解决方案2】:
         Dim read As New System.IO.StreamReader("C:\New Text.txt")
    //I usually only do C#, but rough VB.NEt code: I did not rcreate your array for you, figured you got that :-)
           Dim a As String
           Dim myConcatString as String
           For Each a In read.ReadLine
              Dim myReadLine as String 
                myReadLine = read.ReadLine
               If myReadLine.Equals("RECORD") Then
    myConcatString = myConcatString  & myReadLine 
                   \\How do I read next 3 lines and put them in one array with comma delimiter
               else
               //add myConcatString to array if not null....
    myConcatString =""
               End If
           Next
    

    【讨论】:

    • 我没有硬编码“三”行,因为你真的打破了每次出现的“记录”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 2016-10-19
    相关资源
    最近更新 更多