C#大文件的拷贝

大文件拷贝原理:向内存申请1M空间,反复从源文件读取1M内容写入到目标文件,直到读完。

 1   private void CopyBigFile()
 2         {
 3             string originalPath = @"E:\AdvanceCSharpProject\LearnCSharp\21.zip";
 4             string destPath = @"F:\BaiduNetdiskDownload\21.zip";
 5             //定义读文件流
 6             using (FileStream fsr = new FileStream(originalPath, FileMode.Open))
 7             {
 8                 //定义写文件流
 9                 using (FileStream fsw = new FileStream(destPath, FileMode.OpenOrCreate))
10                 {
11                     //申请1M内存空间
12                     byte[] buffer = new byte[1024 * 1024];
13                     //无限循环中反复读写,直到读完写完
14                     while(true)
15                     {
16                         int readCount = fsr.Read(buffer, 0, buffer.Length);
17                         fsw.Write(buffer, 0, readCount);
18                         if(readCount < buffer.Length)
19                         {
20                             break;
21                         }
22                     }
23                 }
24             }
25         }

 

posted @ 2019-01-01 16:03  _清风明月  阅读(1059)  评论(0编辑  收藏  举报