分离大文件并根据字符串插入回车符

时间:2015-10-01 09:59:10

标签: vb.net carriage-return

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

评论后发生的变化..由于内存限制而下降到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 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

1 个答案:

答案 0 :(得分:0)

由于你的线条很大,你必须:

  • 一次读/写一个字符
  • 保存最后x个字符
  • 如果最后x个字符等于您的字词,请写一个新行

    Dim term As String = "</ext>"
    Dim lastChars As String = "".PadRight(term.Length)
    
    Using sw As StreamWriter = New StreamWriter("C:\My Documents\output.txt")
        Using sr As New System.IO.StreamReader("C:\My Documents\input.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文件。假设每个字符都是一个字节。