using System; using System.IO; //引用命名空间 namespace ConsoleApp { class Program { static void Main(string[] args) { string path = @"C:\Users\Administrator\Desktop\西虹市首富.mp4"; //原文件路径 string newPath = @"C:\Users\Administrator\Desktop\西虹市首富(副本).mp4";//复制到的新文件路径 CopyFile(path, newPath); Console.WriteLine("写入成功"); Console.ReadKey(); } public static void CopyFile(string path, string newPath) { //创建一个负责读取文件的流 using (FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read)) { //创建一个负责写入的流 using (FileStream fsWrite = new FileStream(newPath, FileMode.OpenOrCreate, FileAccess.Write)) { byte[] arr = new byte[1024 * 1024 * 5]; //5兆 int num = 0; //因为文件可能会比较大,所以读取的时候应该通过一个循环去读取 while (true) { // num 返回本次实际读取到的字节数 num = fsRead.Read(arr, 0, arr.Length); //限制读这个数组的长度,也就是5兆5兆的去读取 //如果 num 返回一个0,就意味着什么也没有读取到,文件读取完了 if (num == 0) { break; } //写入 fsWrite.Write(arr, 0, num); } } } } } }
文章评论