SoftWick
SoftWick

Reputation: 43

FileStream faster way to read and write big file

I have a speed problem and memory efficiency, I'm reading the cutting the big chunk of bytes from the .bin files then writing it in another file, the problem is that to read the file i need to create a huge byte array for it:

Dim data3(endOFfile) As Byte ' end of file is here around 270mb size
      Using fs As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None)
        fs.Seek(startOFfile, SeekOrigin.Begin)
        fs.Read(data3, 0, endOFfile)
    End Using
    Using vFs As New FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\test.bin", FileMode.Create) 'save
        vFs.Write(data3, 0, endOFfile)
    End Using

so it takes a long time to procedure, what's the more efficient way to do it? Can I somehow read and write in the same file stream without using a bytes array?

Upvotes: 2

Views: 1204

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54417

I've never done it this way but I would think that the Stream.CopyTo method should be the easiest method and as quick as anything.

Using inputStream As New FileStream(...),
      outputStream As New FileStream(...)
    inputStream.CopyTo(outputStream)
End Using

I'm not sure whether that overload will read all the data in one go or use a default buffer size. If it's the former or you want to specify a buffer size other than the default, there's an overload for that:

inputStream.CopyTo(outputStream, bufferSize)

You can experiment with different buffer sizes to see whether it makes a difference to performance. Smaller is better for memory usage but I would expect bigger to be faster, at least up to a point.

Note that the CopyTo method requires at least .NET Framework 4.0. If you're executing this code on the UI thread, you might like to call CopyToAsync instead, to avoid freezing the UI. The same two overloads are available, plus a third that accepts a CancellationToken. I'm not going to teach you how to use Async/Await here, so research that yourself if you want to go that way. Note that CopyToAsync requires at least .NET Framework 4.5.

Upvotes: 3

Related Questions