VB.Net是新手,但一位朋友建议我用它来做我想做的事情。我有一个巨大的文本文件,我想在一个特定的字符串之后插入回车。
除了下面的混乱之外,我如何修改它来读取一个文件,然后一旦我们看到文本"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 Documents\input.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 Documents\output.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
在评论之后所作的修改。由于内存限制而掉到500 to文件上:
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 Documents\input.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 Documents\output.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
发布于 2015-10-01 06:21:55
因为你的台词很大,你必须:
注意:这将不适用于Unicode文件。这假设每个字符都是一个字节。
https://stackoverflow.com/questions/32884674
复制相似问题