【问题标题】:vb.net - how to stream write file to filevb.net - 如何将文件流式写入文件
【发布时间】:2013-05-28 04:35:54
【问题描述】:

我需要一种方法在 vb.net 中将写入从一个文件流式传输到另一个文件,以便不必将整个文件加载到内存中。这就是我想要的:Stream read bytes in file 1 ---> stream write append bytes to file 2.

我将处理多个 GB 的大文件,因此我需要最高效的方式,并且不想将文件的所有内容加载到内存中。

【问题讨论】:

    标签: vb.net file stream


    【解决方案1】:

    这是一个使用字节数组缓冲区以“块”形式读取和写入文件的简单示例。您可以决定缓冲区的大小:

        Dim bytesRead As Integer
        Dim buffer(4096) As Byte
        Using inFile As New System.IO.FileStream("c:\some path\folder\file1.ext", IO.FileMode.Open, IO.FileAccess.Read)
            Using outFile As New System.IO.FileStream("c:\some path\folder\file2.ext", IO.FileMode.Create, IO.FileAccess.Write)
                Do
                    bytesRead = inFile.Read(buffer, 0, buffer.Length)
                    If bytesRead > 0 Then
                        outFile.Write(buffer, 0, bytesRead)
                    End If
                Loop While bytesRead > 0
            End Using
        End Using
    

    【讨论】:

    • 最大读取量将是缓冲区的大小。还是您的意思是您只想复制文件的一部分然后停止?
    • 知道了...看看你的其他问题。
    猜你喜欢
    • 1970-01-01
    • 2012-03-23
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    相关资源
    最近更新 更多