Csharp/CSharp Tutorial/File Directory Stream/BufferedStream — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:31, 26 мая 2010
Add a buffering layer to FileStream
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();
}
}
Buffered IO with BufferedStream
using System;
using System.IO;
class Tester
{
public static void Main()
{
int SizeBuff = 1024;
Stream inputStream = File.OpenRead("folder3.cs");
Stream outputStream = File.OpenWrite("folder3.bak");
BufferedStream bufferedInput = new BufferedStream(inputStream);
BufferedStream bufferedOutput = new BufferedStream(outputStream);
byte[] buffer = new Byte[SizeBuff];
int bytesRead;
while ((bytesRead =
bufferedInput.Read(buffer, 0, SizeBuff)) > 0)
{
bufferedOutput.Write(buffer, 0, bytesRead);
}
bufferedOutput.Flush();
bufferedInput.Close();
bufferedOutput.Close();
}
}
Use BufferedStream
using System;
using System.IO;
public class MainClass
{
public static int Main(string[] args)
{
FileStream dumpFile = new FileStream("Dump.dat", FileMode.Create, FileAccess.ReadWrite);
BufferedStream myFileBuffer = new BufferedStream(dumpFile);
byte[] str = {127, 0x77, 0x4, 0x0, 0x0, 0x16};
myFileBuffer.Write(str, 0, str.Length);
// add changes to file.
myFileBuffer.Close(); // Flushes.
dumpFile.Close(); // Flushes.
return 0;
}
}