FileStream:是以文件流的形式读写,可适用于所有类型的文件;
StreamRead:也可以读写文件,读写文本文件;
【FileStream以流的形式读写】
static void Main(string[] args)
{
using (FileStream fssource = new FileStream(@"path1", FileMode.Open, FileAccess.Read))
{
using (FileStream fsout = new FileStream(@"path2", FileMode.Create, FileAccess.Write))
{
byte[] bt = new byte[1024 * 1024 * 10];
int res = 0;
while ((res = fssource.Read(bt, 0, bt.Length)) > 0)
{
fsout.Write(bt, 0, res);
}
}
}
}
【StreamReader两种读取方式】:
StreamReader sr = new StreamReader("test.txt", Encoding.Default);
while (!sr.EndOfStream)
{
Console.WriteLine(sr.ReadLine());
}
Console.WriteLine("-----------------");
StreamReader sr1 = new StreamReader("test.txt", Encoding.Default);
string mys=string.Empty;
while ((mys=sr1.ReadLine()) != null)
{
Console.WriteLine(mys);
}
Console.ReadKey(); |