Chinaunix首页 | 论坛 | 博客
  • 博客访问: 443450
  • 博文数量: 155
  • 博客积分: 786
  • 博客等级: 军士长
  • 技术积分: 1561
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-01 23:37
个人简介

在路上

文章分类

全部博文(155)

文章存档

2016年(2)

2015年(36)

2014年(45)

2013年(34)

2012年(38)

我的朋友

分类: C#/.net

2015-08-25 22:51:24


点击(此处)折叠或打开

  1. namespace _07使用文件流来实现多媒体文件的复制
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             //思路:就是先将要复制的多媒体文件读取出来,然后再写入到你指定的位置
  8.             string source = @"C:\Users\SpringRain\Desktop\1、复习.wmv";
  9.             string target = @"C:\Users\SpringRain\Desktop\new.wmv";
  10.             CopyFile(source, target);
  11.             Console.WriteLine("复制成功");
  12.             Console.ReadKey();
  13.         }

  14.         public static void CopyFile(string soucre, string target)
  15.         {
  16.             //1、我们创建一个负责读取的流
  17.             using (FileStream fsRead = new FileStream(soucre, FileMode.Open, FileAccess.Read))
  18.             {
  19.                 //2、创建一个负责写入的流
  20.                 using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
  21.                 {
  22.                     byte[] buffer = new byte[1024 * 1024 * 5];
  23.                     //因为文件可能会比较大,所以我们在读取的时候 应该通过一个循环去读取
  24.                     while (true)
  25.                     {
  26.                         //返回本次读取实际读取到的字节数
  27.                         int r = fsRead.Read(buffer, 0, buffer.Length);
  28.                         //如果返回一个0,也就意味什么都没有读取到,读取完了
  29.                         if (r == 0)
  30.                         {
  31.                             break;
  32.                         }
  33.                         fsWrite.Write(buffer, 0, r);
  34.                     }

  35.                  
  36.                 }
  37.             }
  38.         }


  39.     }
  40. }

阅读(653) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~