【问题标题】:Split the records in text file拆分文本文件中的记录
【发布时间】:2019-06-03 01:33:49
【问题描述】:

我正在开发 vb.net 应用程序。其中我有多个文本文件,需要根据文件中的某些标识符(重复词)拆分记录。 你能帮我吗,因为我是 vb.net 的新手,不知道该怎么做。 到目前为止我已经编码了

If (Directory.Exists(filePath)) Then
            'search file in the input path by their search pattern
            For Each File As String In Directory.GetFiles(filePath, "*.txt", SearchOption.TopDirectoryOnly)

                Console.WriteLine("Reading the current file " + Path.GetFileName(File))
                Using sr As StreamReader = New StreamReader(File)
                    Dim Currentline As String
                    Dim Identifier As String
                    Dim statementDate As String
                    Dim currenttext As String

                    'getting the unique identifier from the files and removing the white spaces
                    Identifier = sr.ReadLine.Substring(69, 8)
                    'checks until the EOF
                    While Not sr.EndOfStream

                        currenttext = sr.ReadLine()
                        'loop through until identified not repeated
                        Do Until currenttext.Contains(Identifier)

                            Currentline = sr.ReadLine()
                            Console.WriteLine(Currentline)


                        Loop
                        Console.WriteLine("=========================== Records Ends")

                    End While
                End Using

另外,这里是需要拆分的文本文件的屏幕截图。

提前致谢。

【问题讨论】:

    标签: vb.net console


    【解决方案1】:

    在这个例子中,我制作了多个文本文件。希望对您有所帮助。

    附言将Identifier = Mid(sr.ReadLine, 1, 5) 更改为Identifier = Mid(sr.ReadLine, 69, 8)

    再见

     If (Directory.Exists(filePath)) Then
    
            Try
                'search file in the input path by their search pattern
                For Each File As String In Directory.GetFiles(filePath, "*.txt", SearchOption.TopDirectoryOnly)
    
                    Console.WriteLine("Reading the current file " + Path.GetFileName(File))
                    Using sr As StreamReader = New StreamReader(File)
                        Dim Currentline As String = ""
                        Dim Identifier As String = ""
                        Dim currenttext As String = ""
                        Dim Prog As Integer = 0
                        Dim flg As Boolean = True
    
                        While Not sr.EndOfStream
    
                            'getting the unique identifier from the files and removing the white spaces
                            Identifier = Mid(sr.ReadLine, 1, 5)
    
                            Do While Not sr.EndOfStream
    
                                Do While flg = True
                                    Currentline = sr.ReadLine()
                                    If Identifier = Currentline.Trim Then
                                        Exit Do
                                    ElseIf sr.EndOfStream Then
                                        currenttext = currenttext + Currentline + vbCrLf
                                        Exit Do
                                    End If
                                    currenttext = currenttext + Currentline + vbCrLf
                                Loop
    
                                currenttext = currenttext + "=========================== Records Ends"
    
                                Prog += 1
                                Dim objWriter As New System.IO.StreamWriter(filePath + "\" + Path.GetFileName(File) + "_" + Prog.ToString + ".txt")
                                objWriter.WriteLine(currenttext)
                                objWriter.Close()
                                currenttext = ""
                            Loop
    
                        End While
    
                    End Using
    
                Next
    
                MessageBox.Show("end")
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
        End If
    

    【讨论】:

      【解决方案2】:

      这应该适合你....

      Imports System.IO
      Imports System.Text
      
      Sub Main()
          If (Directory.Exists(filePath)) Then
              For Each File As String In Directory.GetFiles(filePath, "*.txt", SearchOption.TopDirectoryOnly)
                  Dim Record As New StringBuilder
                  Dim Identifier As String = String.Empty
      
                  Debug.Print("Reading the current file {0}", Path.GetFileName(File))
                  Using sr As StreamReader = New StreamReader(File)
                      While Not sr.EndOfStream
                          Dim ThisLine As String = sr.ReadLine.Trim
      
                          Select Case True
                              Case ThisLine.Length = 0
                                  ' Skip blank lines
                              Case Identifier.Length = 0
                                  ' We need to set the Identifier
                                  Identifier = ThisLine
                              Case ThisLine = Identifier
                                  ' We have the whole record
                                  ProcessRecord(Record.ToString.Trim)
      
                                  ' Reset for next record
                                  Record.Clear()
                              Case Else
                                  ' Add this line to the current record
                                  Record.AppendLine(ThisLine)
                          End Select
                      End While
      
                      ' Process last record in file
                      ProcessRecord(Record.ToString.Trim)
                  End Using
      
                  Debug.Print("=========================== File Ends")
              Next
          End If
      End Sub
      
      Sub ProcessRecord(Record As String)
          If Record.Length > 0 Then
              Debug.Print(Record)
              Debug.Print("=========================== Record Ends")
          End If
      End Sub
      

      原答案如下

      If (Directory.Exists(filePath)) Then
          For Each File As String In Directory.GetFiles(filePath, "*.txt", SearchOption.TopDirectoryOnly)
              Dim AllLines() As String = IO.File.ReadAllLines(File)
              Dim Identifier As String = AllLines.First
              Dim Records() As String = Split(Join(AllLines, Environment.NewLine), Identifier)
      
              For Each Rec As String In Records
                  Debug.Print(Rec)
                  Debug.Print("=========================== Record Ends")
              Next
          Next
      
          Debug.Print("=========================== File Ends")
      End If
      

      【讨论】:

      • 我不想使用 ReadAllLines。因为它将文件加载到内存中并且可能是内存问题。使用流式阅读器可以做到这一点
      • @VirenderThakur 我已经修改了答案。
      猜你喜欢
      • 2018-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多