【发布时间】:2013-05-28 04:35:54
【问题描述】:
我需要一种方法在 vb.net 中将写入从一个文件流式传输到另一个文件,以便不必将整个文件加载到内存中。这就是我想要的:Stream read bytes in file 1 ---> stream write append bytes to file 2.
我将处理多个 GB 的大文件,因此我需要最高效的方式,并且不想将文件的所有内容加载到内存中。
【问题讨论】:
我需要一种方法在 vb.net 中将写入从一个文件流式传输到另一个文件,以便不必将整个文件加载到内存中。这就是我想要的:Stream read bytes in file 1 ---> stream write append bytes to file 2.
我将处理多个 GB 的大文件,因此我需要最高效的方式,并且不想将文件的所有内容加载到内存中。
【问题讨论】:
这是一个使用字节数组缓冲区以“块”形式读取和写入文件的简单示例。您可以决定缓冲区的大小:
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
【讨论】: