If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Read part from a binary file in chunks

Hey everybody.

Does anybody know how to read a part of a binary file and write it to a new file in smaller chunks?

I know it can be done with the code below, but the data I need to read/write is rather large, so I'd like to do it in smaller chunks, so it won't use a lot of memory. The only examples I can find of doing it in chunks, is when reading/writing an entire file from start to end. Basically I'd like to feed it a start position and the number of bytes that should be read and that part of data is read and written to a new file in smaller chunks of 1024 or 2048 bytes.

Code:

Using fIn As New FileStream("C:\filetoread.dat", FileMode.Open, FileAccess.Read)
fIn.Position = 41395517
Using fOut As New FileStream("C:\filetowrite.dat", FileMode.Create, FileAccess.Write)
Dim btArray(27846842) As Byte
Dim NumBytes As Integer = fIn.Read(btArray, 0, btArray.Length)
fOut.Write(btArray, 0, NumBytes)
End Using
End Using