Add a buffering layer to FileStream : BufferedStream « File Directory Stream « C# / CSharp Tutorial






using System;
using System.Windows.Forms;
using System.IO;

class MainClass
{
  public static void Main() 
  {
    FileStream inStream = File.OpenRead("c:\\text.txt");
    FileStream outStream = File.OpenWrite("c:\\text.txt" + ".bak");

    
    BufferedStream bufInStream = new BufferedStream(inStream);
    BufferedStream bufOutStream = new BufferedStream(outStream);

    byte[] buf = new byte[1024];
    int bytesRead;

    while ((bytesRead = bufInStream.Read(buf, 0, 1024)) > 0)
      bufOutStream.Write(buf, 0, bytesRead);

    bufOutStream.Flush();
    bufOutStream.Close();
    bufInStream.Close();
    outStream.Close();
    inStream.Close();
  }
}








15.24.BufferedStream
15.24.1.Use BufferedStream
15.24.2.Add a buffering layer to FileStream
15.24.3.Buffered IO with BufferedStream