vb.net - 如何将写入文件流式传输到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16872813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vb.net - how to stream write file to file
提问by Daniel Valland
I need a way to stream write from one file to the other in vb.net so that the entire files don`t have to be loaded in memory. Here is what I want: Stream read bytes in file 1 ---> stream write append bytes to file 2.
我需要一种在 vb.net 中从一个文件流写入另一个文件的方法,以便不必将整个文件加载到内存中。这是我想要的:流读取文件 1 中的字节 ---> 流写入附加字节到文件 2。
I will be working with large files, multiple GB, so I need the most effiant way of doing it, and don`t want to load all content of the file to memory.
我将使用大文件,多个 GB,所以我需要最有效的方法,并且不想将文件的所有内容加载到内存中。
回答by Idle_Mind
Here's a simple example of reading and writing the files in "chunks" using a byte array buffer. You can decide how big to make the buffer:
这是使用字节数组缓冲区读取和写入“块”中的文件的简单示例。您可以决定缓冲区的大小:
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

