标签:
大文件拷贝:
1 /// <summary> 2 /// 大文件拷贝 3 /// </summary> 4 /// <param name="sSource"></param> 5 /// <param name="sTarget"></param> 6 private static void CopyFile(string sSource, string sTarget) 7 { 8 using (FileStream fsRead=new FileStream(sSource,FileMode.Open,FileAccess.Read)) 9 { 10 using (FileStream fsWrite=new FileStream(sTarget,FileMode.Create,FileAccess.Write)) 11 { 12 double n = (double)fsRead.Length; 13 Stopwatch sw = new Stopwatch(); 14 15 sw.Start(); 16 while (true) 17 { 18 //定义一个6MB的缓冲区 19 byte[] buffer = new byte[6 * 1024 * 1024]; 20 int m = fsRead.Read(buffer, 0, buffer.Length); 21 22 if (m==0) 23 { 24 break; 25 } 26 else 27 { 28 fsWrite.Write(buffer, 0, m); 29 double dl = Math.Round(((double)fsWrite.Position / n) * 100, 1); 30 Console.WriteLine("总进度:{0}%", dl); 31 } 32 } 33 sw.Stop(); 34 double iTime = Math.Round((double)sw.ElapsedMilliseconds / 1000, 2); 35 Console.WriteLine("总运行时间:"+iTime+"秒"); 36 } 37 } 38 }
FileStream是对字节(byte)的操作,上面这种方法拷贝电影文件,大的压缩文件等没问题,但是对于文本文件多半使用后面2个类StreamReader,StreamWriter,
1 #region 使用StreamWriter进行写 2 using (StreamWriter sw = new StreamWriter(@"D:\demo.txt")) 3 { 4 for (int i = 1; i <= 20; i++) 5 { 6 sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd") + "\t" + i.ToString()); 7 } 8 } 9 Console.WriteLine("OK"); 10 #endregion
1 #region 使用StreamReader进行读 2 using (StreamReader sr = new StreamReader(@"D:\demo.txt")) 3 { 4 string s = sr.ReadLine(); 5 while (!string.IsNullOrEmpty(s)) 6 { 7 Console.WriteLine(s); 8 s = sr.ReadLine(); 9 } 10 } 11 #endregion
文件流操作(FileStream,StreamReader,StreamWriter)
标签:
原文地址:http://www.cnblogs.com/chens2865/p/4537013.html