289 字节流复制视频

36 篇文章 0 订阅

289 字节流复制视频

【需求】

把"C:\develper\smile.mp4"复制到"myIOStream\src\e289\111.mp4"(把一个视频文件,从磁盘目录,复制到模块目录)

【思路】

  1. 根据数据源创建字节输入流对象
  2. 根据目的地创建字节输出流对象
  3. 读写数据,实现复制视频
  4. 释放资源

【要求】

使用4种方式复制,并记录每一种所需的时间

  1. 字节流 一次读写一个字节
  2. 字节流 一次读写一个字节 数组
  3. 字节缓冲流 一次读写一个字节
  4. 字节缓冲流 一次读写一个字节 数组

【报错经验】

close的对象不能错,比如bis.close()

close的位置不能错

应该写参数不能忘了写,比如fis.read(b)

【报错了】

和e288相似的代码,不同的问题。

byte[] b=new byte[1024];

        int i;

        while ((i=fis.read())!=-1){

            fos.write(b,0,i);

        }

原文件120k,运行后,产生一个15m的MP4文件,且无法正常观看

原因:

while ((i=fis.read(b))!=-1){

            fos.write(b,0,i);

        }

fis.read(b)的b没写上

--------------------------------------------------------------

1 package e289;2

3 import java.io.*;4

5 public class CopyVideoDemo {

6     public static void main(String[] args) throws IOException {

7         method1();

8         method2();

9         method3();

10         method4();

11     }

12

13     public static void method1() throws IOException {

14         FileInputStream fis = new FileInputStream("C:\\develper\\smile.mp4");

15         FileOutputStream fos = new FileOutputStream("myIOStream\\src\\e289\\111.mp4");

16         long beginT = System.currentTimeMillis();

17         long endT = System.currentTimeMillis();

//beginT、endT是为了统计所用时长

//字节流按字节复制数据,fis的路径指向已存在的文件

18         int i;

19         while ((i = fis.read()) != -1) {

20             fos.write(i);

21         }

22         System.out.println("METHOD1:\t" + (endT - beginT) + "\t" + beginT + "-" + endT);

23         fos.close();

24         fis.close();

25     }

26

27     public static void method2() throws IOException {

28         FileInputStream fis = new FileInputStream("C:\\develper\\smile.mp4");

29         FileOutputStream fos = new FileOutputStream("myIOStream\\src\\e289\\222.mp4");

30         long beginT = System.currentTimeMillis();

31         long endT = System.currentTimeMillis();

//字节流按数组复制数据

33         byte[] b = new byte[1024];

34         int i;

35         while ((i = fis.read(b)) != -1) {

36             fos.write(b, 0, i);

37         }

38         System.out.println("METHOD2:\t" + (endT - beginT) + "\t" + beginT + "-" + endT);

39         fis.close();

40         fos.close();

41     }

42

43     public static void method3() throws IOException {

44         FileInputStream fis = new FileInputStream("C:\\develper\\smile.mp4");

45         BufferedInputStream bis = new BufferedInputStream(fis);

46         FileOutputStream fos = new FileOutputStream("myIOStream\\src\\e289\\333.mp4");

47         BufferedOutputStream bos = new BufferedOutputStream(fos);

48         long beginT = System.currentTimeMillis();

49         long endT = System.currentTimeMillis();

50

51         int i;

52         while ((i = bis.read()) != -1) {

53             bos.write(i);

54         }

55         System.out.println("METHOD3:\t" + (endT - beginT) + "\t" + beginT + "-" + endT);

56         bis.close();

57         bos.close();

58     }

59

60     public static void method4() throws IOException {

61         FileInputStream fis = new FileInputStream("C:\\develper\\smile.mp4");

62         BufferedInputStream bis = new BufferedInputStream(fis);

63         FileOutputStream fos = new FileOutputStream("myIOStream\\src\\e289\\444.mp4");

64         BufferedOutputStream bos = new BufferedOutputStream(fos);

65         long beginT = System.currentTimeMillis();

66         long endT = System.currentTimeMillis();

67

68         byte[] b = new byte[1024];

69         int i;

70         while ((i = bis.read(b)) != -1) {

71             bos.write(b, 0, i);

72         }

73         System.out.println("METHOD4:\t" + (endT - beginT) + "\t" + beginT + "-" + endT);

74         bis.close();

75         bos.close();

76     }

77 }

78 /*

79 METHOD1:   0  1633621599835-1633621599835

80 METHOD2:   0  1633621602034-1633621602034

81 METHOD3:   0  1633621602038-1633621602038

82 METHOD4:   0  1633621602053-1633621602053

83  */

【method4思路】

要有bis,要有bos

创建数组、变量

while的条件是变量不等于-1,变量的值来自bis.read(数组)

while内部是bos写数据,bos.write的参数是3个:数组名、0、变量名

别忘了close

【报错了】

byte[] b=new byte[1024];

        int i;

        while ((i=fis.read())!=-1){

            fos.write(b,0,i);

        }

原文件120k,运行后,产生一个15m的MP4文件,且无法正常观看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值