FileStream、StreamReader和StreamWriter可以操作大文件;
FileStream 操作字節;可以操作任何類型的文件;
StreamReader和StreamWriter操作字符;
FileMode OpenOrCreate , Append
FileAcess. Read 、Write、ReadWirte
將創建文件流對象的過程寫在using當中,會自動幫助我們釋放資源;
可以用來讀取格式化文本文件;
有ReadLine和Write WriteLine方法
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 用FileStream讀寫文件
{
class Program
{
static void Main(string[] args)
{
String str = @"E:\下載\軟件";
Stopwatch sw = new Stopwatch();
sw.Start();
using (FileStream fsWriter = new FileStream(str + @"\opencv-3.0.exe", FileMode.Create, FileAccess.Write))
{
using (FileStream fsReader = new FileStream(str + @"\opencv-2.4.9.exe", FileMode.Open, FileAccess.Read))
{
byte[] bytes=new byte[1024*4];//4kB是合適的;
int readNum;
while((readNum=fsReader.Read(bytes,0,bytes.Length))!=0)//小於說明讀完了
{
fsWriter.Write(bytes,0,readNum);
}
}//suing reader
}//using writer
sw.Stop();
Console.WriteLine("總的運行時間為{0}",sw.ElapsedMilliseconds);
Console.ReadKey();
}//main
}//class
}