2016-09-30 84 views
0

我遇到了一個問題,我需要逐行讀取文本文件,並將每行放入一個字符串或其他符合條件的文件中。我遇到的問題是它花了很長時間,我只是想知道是否有更快捷的方式來做事。我已經做了大量的研究,如何做到這一點,這是我能想到的最好的。謝謝。 (每次追加兩個字符串,因爲不得不將這兩個字符串直接輸出到文本文件)。通過文本文件逐行讀取異常長時間的文本文件

內容位於一個巨大的文本文件中,其中一條信息從以「aaa」開頭的行開始。我必須通過查找分隔這些信息段的文本文件來查看以「aaa」開頭的行。將一條信息與fullStr1或fullStr2分開的標準是,索引29處的字符是空格(「 「) 或不。謝謝。

 Using reader As StreamReader = New StreamReader(file) 
      Dim line As String = reader.ReadLine 
      Do While (Not line Is Nothing) 
       If line.Substring(0, 3) = "aaa" AndAlso line.Substring(29, 1) <> " " Then 
        Do 
         fullStr1 = fullStr1 & line & vbCrLf 
         line = reader.ReadLine 
        Loop While (Not line Is Nothing AndAlso line.Substring(0, 3) <> "aaa") 
       ElseIf line.Substring(0, 3) = "aaa" AndAlso line.Substring(29, 1) = " " Then 
        Do 
         fullStr2 = fullStr2 & line & vbCrLf 
         line = reader.ReadLine 
        Loop While (Not line Is Nothing AndAlso line.Substring(0, 3) <> "aaa") 
       End If 
      Loop 
     End Using 

回答

0

一個非常快速和簡單的解決方案是使用StringBuilder的類型,而不是一個String類型變量線和fullStr。 (見https://msdn.microsoft.com/en-us/library/ms172824.aspx)。 字符串是不可變的,這意味着每次爲line或fullStr變量賦值時,都沒有真正更新內存中變量的值,而是取消先前分配的內存併爲其分配新的內存空間變量並將新值分配給新的內存空間。這是很大的開銷,會影響應用程序的性能。

+0

感謝一旦你提到了StringBuilder我已經完成了分鐘,關閉時間的光陰。 – AF1001

3

如果您有足夠長的文本文件,無論您做什麼,掃描都需要一段時間。但有一件事你可以做一點幫助,就是使用StringBuilder。它正是爲了這種情況而準備的,比將一個巨大的字符串逐個拼接起來效率更高。

Dim builder1 As New StringBuilder() 
Dim builder2 As New StringBuilder() 

Using reader As StreamReader = New StreamReader(file) 
    Dim line As String = reader.ReadLine 
    Do While (Not line Is Nothing) 
     If line.Substring(0, 3) = "aaa" AndAlso line.Substring(29, 1) <> " " Then 
     Do 
      builder1.AppendLine(line) 
      line = reader.ReadLine 
     Loop While (Not line Is Nothing AndAlso line.Substring(0, 3) <> "aaa") 
     ElseIf line.Substring(0, 3) = "aaa" AndAlso line.Substring(29, 1) = " " Then 
     Do 
      builder2.AppendLine(line) 
      line = reader.ReadLine 
     Loop While (Not line Is Nothing AndAlso line.Substring(0, 3) <> "aaa") 
     End If 
    Loop 
End Using 

我也很小心,你的循環沒有得到「卡殼」,因爲在外環沒有reader.ReadLine電話。如果它進入外部循環並且當前行不是以「aaa」開頭的情況,那麼它將永遠循環無用。你所需要做的就是在第一行沒有「aaa」。

+0

+1。字符串實例的不斷重新分配和銷燬很可能是緩慢的罪魁禍首。 – Joey

0

我知道這不是最好的解決方案,但它可能會更快一點。我總是把每一行放在一個列表中。這有助於使字符串不那麼長,然後我可以逐行進行。但是當你逐行閱讀時,任何更大的文件都會讀取文件的每行/每個字節,因此需要花費一些時間。

Private Function ReadInFile(ByVal strFile As String) As List(Of String) 
    Dim strLineTemp As String = "" 
    Using read As New StreamReader(strFile) 
     While read.Peek <> -1 
      strLineTemp = read.ReadLine() 
      If strLineTemp.Trim <> "" Then 
       lstFileData.Add(strLineTemp) 
      End If 
     End While 
    End Using 
    Return lstFileData 
End Function 
0

這是你想要的嗎?我交換條件使用正則表達式,並刪除內部循環。雖然我會交換'StringBuilder'的字符串concatination,但是我在一個大文件中耗盡了內存。

Dim regex As Regex = New Regex("aaa.{25} .*") 
    st.Start() 
    Using reader As StreamReader = New StreamReader(file) 
     Dim line As String = reader.ReadLine 
     Do While (Not line Is Nothing) 
      If regex.Match(line).Success = False Then 

       fullStr1 = fullStr1 & line & vbCrLf 
       line = reader.ReadLine 
      Else 
       fullStr2 = fullStr2 & line & vbCrLf 
       line = reader.ReadLine 

      End If 

     Loop 

    End Using