Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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_Split_Streamreader_Streamwriter_Filesplitting - Fatal编程技术网

Vb.net 如何提高简单文件拆分程序的效率

Vb.net 如何提高简单文件拆分程序的效率,vb.net,split,streamreader,streamwriter,filesplitting,Vb.net,Split,Streamreader,Streamwriter,Filesplitting,我有一个简单的程序,可以读取一个.txt文件,然后将其拆分为多个“pMaxRows”行数的文件。这些.txt文件非常庞大,有些文件接近25Gb。现在它的运行速度还不够快,我觉得应该有一种方法可以通过一次读/写多行来提高效率,但我对vb.net streamreader/streamwriter不是很有经验 代码如下: Public Sub Execute(ByVal pFileLocation As String, _ ByVal pMaxRows As I

我有一个简单的程序,可以读取一个.txt文件,然后将其拆分为多个“pMaxRows”行数的文件。这些.txt文件非常庞大,有些文件接近25Gb。现在它的运行速度还不够快,我觉得应该有一种方法可以通过一次读/写多行来提高效率,但我对vb.net streamreader/streamwriter不是很有经验

代码如下:

Public Sub Execute(ByVal pFileLocation As String, _
                   ByVal pMaxRows As Int32)

    Dim sr As IO.StreamReader
    Dim Row As String
    Dim SourceRowCount As Int64
    Dim TargetRowCount As int64
    Dim TargetFileNumber As Int32
    ''Does the file exist in that location?
    If IO.File.Exists(pFileLocation) = False Then
        Throw New Exception("File does not exist at " & pFileLocation)
    End If

    ''Split FileLocation into FileName and Folder Location
    Dim arrFileLoc() As String = pFileLocation.Split("\")
    Dim i As Integer = arrFileLoc.Length - 1
    Dim FileName As String = arrFileLoc(i)
    Dim FileLocationLength As Integer = pFileLocation.Length
    Dim FileNameLength As Integer = FileName.Length
    Dim Folder As String = pFileLocation.Remove(FileLocationLength - FileNameLength, FileNameLength)



    ''Read the file
    sr = New IO.StreamReader(pFileLocation)
    SourceRowCount = 0
    TargetRowCount = 0
    TargetFileNumber = 1


    ''Create First Target File Name
    Dim TargetFileName As String
    TargetFileName = TargetFileNumber & "_" & FileName

    ''Open streamreader and start reading lines
    Do While Not sr.EndOfStream


        ''if it hits the target number of rows: 
        If (TargetRowCount = pMaxRows) Then

            ''Advance target file number
            TargetFileNumber += 1
            ''Create New file with target file number
            TargetFileName = TargetFileNumber & "_" & FileName

            ''Set target row count back to 0
            TargetRowCount = 0

        End If
        ''Read line
        Row = sr.ReadLine()

        ''Write line
        Using sw As New StreamWriter(Folder & TargetFileName, True)
            sw.WriteLine(Row)
        End Using

        SourceRowCount += 1
        TargetRowCount += 1

    Loop
End Sub

有人有什么建议吗?如果之前已经回答过,那么即使将我引向正确的位置,我也会非常感激

如果积累几行代码(比如,从Math.Min(pMaxRows,1000)开始)写入StringBuilder,并且写入这些代码的速度不够快,可能也会被问到,按照LarsTech的建议转到代码审阅。看起来您正在为写入的每一行打开StreamWriter——尝试为每个输出文件只打开一次。您可以读取文件,同时读取一定数量的行(或字符/比特数),并将这些行传递给新线程,然后在不同的线程上执行写入操作。例如,为了不使您的机器负担过重,监视器一次只使用10个线程。我会尝试读取/写入字节块而不是行:将10MB块读取到stringbuilder中,逐字节查找下一个换行符,写入10MB+最后一个行块的剩余部分,重复。如果记录是固定长度的,则更容易。当然,这取决于分割块的标准。可能应该询问是否累积了几行(比如,从Math.Min(pMaxRows,1000)开始)以写入StringBuilder,但写入速度不够快,按照LarsTech的建议转到代码审阅。看起来您正在为写入的每一行打开StreamWriter——尝试为每个输出文件只打开一次。您可以读取文件,同时读取一定数量的行(或字符/比特数),并将这些行传递给新线程,然后在不同的线程上执行写入操作。例如,为了不使您的机器负担过重,监视器一次只使用10个线程。我会尝试读取/写入字节块而不是行:将10MB块读取到stringbuilder中,逐字节查找下一个换行符,写入10MB+最后一个行块的剩余部分,重复。如果记录是固定长度的,则更容易。当然,这取决于分割块的标准。