我需要搜索并替换大的ascii文本文件的2行,其中这可能在随机位置出现n次(n> 1000)。文本文件如下所示:
....
StringVariable='
my contents'
.....
我希望它阅读:
....
StringVariable='my contents'
....
对于小文件,我使用AllText,它适用于小文件:
My.Computer.FileSystem.WriteAllText(MyInputFile, My.Computer.FileSystem.ReadAllText(MyOutputFile).Replace("='" & vbCrLf, "='"), False)
对于大型的,AllText会因内存不足而崩溃。我看到使用ReadLine和WriteLine的帖子,以及如何测试字符串的字符串,但我想知道如何组合多行'n'次而不会丢失我在文件中的位置。我想我可以将大文件小心地拆分成许多小文件以允许使用AllText,然后重新组合,但这似乎很粗糙。有没有更好的办法?
我看到如何修复上面列出的情况,但我有其他情况(例如,在特定字符串之后的2个CR)并且很难解决灵活的情况,你想要用可变长度多替换多行字符串线字符串。
以下是我用于上述初始案例的代码:
Private Sub RemoveCRBefore(ByVal Infile As String, ByVal Outfile As String, ByVal LookedFor As String)
Dim Line0 As String = ""
Dim LinedUp As String = ""
Dim LookLong As Integer = LookedFor.Length
Dim FirstLine As Boolean = True
Using sr As StreamReader = New StreamReader(Infile)
Using sw = System.IO.File.CreateText(Outfile)
Dim Line1 As String = sr.ReadLine
Do While (Not Line1 Is Nothing)
If Line1.Length >= LookLong Then
If LookedFor = Line1.Substring(0, LookLong) And Not FirstLine Then
LinedUp = Line0.Replace(vbCrLf, "") & Line1
Line0 = LinedUp
FirstLine = True
Else
If FirstLine = False Then sw.WriteLine(Line0)
Line0 = Line1
End If
Else
sw.WriteLine(Line0)
Line0 = Line1
End If
Line1 = sr.ReadLine
FirstLine = False
Loop
sw.WriteLine(Line0)
End Using
End Using
End Sub