VB新手。但一个朋友建议我用它来做我想做的事情。我有一个巨大的文本文件,我想在一个特定的字符串后插入回车。
除了下面的混乱,我该如何修改它来读取一个文件,然后一旦我们看到文本"ext"插入一个新的换行。我期望输入文件中的某一行产生大量回车。
目前,我已经设法模拟下面一起读取输入文件,直到行结束,并再次将其写入另一个文件。
Module Module1
Sub Main()
Try
' Create an instance of StreamReader to read from a file.
' The using statement also closes the StreamReader.
Using sr As StreamReader = New StreamReader("C:My Documentsinput.txt")
Dim line As String
' Read and display lines from the file until the end of
' the file is reached.
Using sw As StreamWriter = New StreamWriter("C:My Documentsoutput.txt")
Do Until sr.EndOfStream
line = sr.ReadLine()
sw.WriteLine(line)
Console.WriteLine("done")
Loop
End Using
End Using
Catch e As Exception
' Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(e.Message)
End Try
Console.ReadKey()
End Sub
以下注释所做的更改…由于内存限制,在500mb的文件中失败:
Sub Main()
Try
' Create an instance of StreamReader to read from a file.
' The using statement also closes the StreamReader.
Using sr As StreamReader = New StreamReader("C:My Documentsinput.txt")
Dim line As String
Dim term As String = "</ext>"
' Read and display lines from the file until the end of
' the file is reached.
Using sw As StreamWriter = New StreamWriter("C:My Documentsoutput.txt")
Do Until sr.EndOfStream
line = sr.ReadLine()
line = line.Replace(term, term + Environment.NewLine)
sw.WriteLine(line)
Console.WriteLine("done")
Loop
End Using
End Using
由于你的行很长,你必须:
- 一次读/写一个字符
- 保存最后x个字符
-
如果最后x个字符等于你的术语,写一个新的行
Dim term As String = "</ext>" Dim lastChars As String = "".PadRight(term.Length) Using sw As StreamWriter = New StreamWriter("C:My Documentsoutput.txt") Using sr As New System.IO.StreamReader("C:My Documentsinput.txt") While Not sr.EndOfStream Dim buffer(1) As Char sr.Read(buffer, 0, 1) lastChars &= buffer(0) lastChars = lastChars.Remove(0, 1) sw.Write(buffer(0)) If lastChars = term Then sw.Write(Environment.NewLine) End If End While End Using End Using
注意:这对Unicode文件不起作用。这里假设每个字符是一个字节