Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
替换内存问题VB.net_Vb.net_Memory_Replace_Streamwriter - Fatal编程技术网

替换内存问题VB.net

替换内存问题VB.net,vb.net,memory,replace,streamwriter,Vb.net,Memory,Replace,Streamwriter,我之前在这段代码上得到了很好的帮助,但是我遇到了一个绊脚石,我不确定该怎么做。我有下面的代码,它对超过120k的查找和替换进行查找和替换。问题是文本文件非常大,很容易超过5 gig的日志文件,所以我会遇到内存问题,这并不奇怪。那么,如果可能的话,我是否以块的形式加载数据?如果可能,如何加载 如果输入文件是一个简单的多行文本文件,其中没有一行太大而无法一次加载到内存中,并且搜索字符串永远不会跨越多行,那么一次只读取一行将是最简单的解决方案。例如: Dim fName As String = "c

我之前在这段代码上得到了很好的帮助,但是我遇到了一个绊脚石,我不确定该怎么做。我有下面的代码,它对超过120k的查找和替换进行查找和替换。问题是文本文件非常大,很容易超过5 gig的日志文件,所以我会遇到内存问题,这并不奇怪。那么,如果可能的话,我是否以块的形式加载数据?如果可能,如何加载




如果输入文件是一个简单的多行文本文件,其中没有一行太大而无法一次加载到内存中,并且搜索字符串永远不会跨越多行,那么一次只读取一行将是最简单的解决方案。例如:

Dim fName As String = "c:\backup\logs\master.txt"
Dim wrtFile As String = "c:\backup\logs\masterUserFormatted.txt"
Dim strRead As New System.IO.StreamReader(fName)
Dim strWrite As New System.IO.StreamWriter(wrtFile)
Cursor.Current = Cursors.WaitCursor
While True
    Dim line As String = strRead.ReadLine()
    If line IsNot Nothing Then
        For Each row As DataGridViewRow In DataGridView1.Rows
            If Not row.IsNewRow Then
                Dim Find1 As String = row.Cells(0).Value.ToString
                Dim Replace1 As String = row.Cells(1).Value.ToString
                line = line.Replace(Find1, Replace1)
            End If
        Next
        strWrite.WriteLine(line)
    Else
        Exit While
    End If
End While
strRead.Close()
strWrite.Close()
Cursor.Current = Cursors.Default
MessageBox.Show("Finished Replacing")

值得一提的是,
StreamReader
StreamReader
实现了
IDisposable
。因此,最好使用块将它们封装在
中,而不是自己显式调用
关闭

嗨,史蒂夫,非常感谢你的回复,我已经更新和测试了,但它似乎没有通过查找和替换来运行,因为有超过100k的代码。您好,@vbvirg20我已经更新了我的答案,包括一个更完整的例子。史蒂文:你是一个明星,谢谢你的帮助和解释,显然我需要在我的循环上做更多的工作。再次感谢,您在(现已删除的)“answer”中发布的最后一个示例的问题是错误的,因为它首先遍历网格中的所有搜索/替换字符串,然后对每个字符串遍历文件以执行当前替换。唯一可行的方法是关闭文件,然后打开上一个输出文件,将其用作下一个循环的输入文件(这将非常低效)。我在回答中显示的方法有效,因为它循环文件中的每一行,然后循环每一行,它循环遍历网格中的所有搜索/替换字符串,并在转到下一行之前将它们全部应用于该行。这样,它只在文件中循环一次,效率要高得多。读取网格中的字符串非常快,因为它都在RAM中。由于文件位于磁盘上,因此读取和写入文件的速度要慢得多。
Dim fName As String = "c:\backup\logs\master.txt"
Dim wrtFile As String = "c:\backup\logs\masterUserFormatted.txt"
Dim strRead As New System.IO.StreamReader(fName)
Dim strWrite As New System.IO.StreamWriter(wrtFile)
Cursor.Current = Cursors.WaitCursor
While True
    Dim line As String = strRead.ReadLine()
    If line IsNot Nothing Then
        For Each row As DataGridViewRow In DataGridView1.Rows
            If Not row.IsNewRow Then
                Dim Find1 As String = row.Cells(0).Value.ToString
                Dim Replace1 As String = row.Cells(1).Value.ToString
                line = line.Replace(Find1, Replace1)
            End If
        Next
        strWrite.WriteLine(line)
    Else
        Exit While
    End If
End While
strRead.Close()
strWrite.Close()
Cursor.Current = Cursors.Default
MessageBox.Show("Finished Replacing")