Sử dụng bufferedStream trong C#

Sử dụng bufferedStream để tăng tốc độ đọc ghi file với kích thước lớn, thay vì sử dụng buffer mặc định 4096 byte.

public class BufferedStreamTest
{

private const int buffSize = 4096;

public static void Run()
{

string inputFile = Path.GetFullPath(“..\\..\\”) + “\\TextFile.cs”;
string outputFile = Path.GetFullPath(“..\\..\\”) + “\\TextFile.bak”;

FileStream inputStream = new FileStream(inputFile,FileMode.Open,FileAccess.Read);
//Stream inputStream = File.OpenRead(inputFile);

FileStream outputStream = new FileStream(outputFile,FileMode.OpenOrCreate,FileAccess.Write);
//Stream outputStream = File.OpenWrite(inputFile);

BufferedStream bufferedInput = new BufferedStream(inputStream);

BufferedStream bufferedOutput = new BufferedStream(outputStream);
byte[] buff = new byte[buffSize];

int readBytes;
while((readBytes=bufferedInput.Read(buff,0,buffSize))>0)
{

bufferedOutput.Write(buff,0,readBytes);
Console.WriteLine(buff);

}
bufferedInput.Close();
bufferedOutput.Flush();
bufferedOutput.Close();

}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s