289 字节流复制视频
【需求】
把"C:\develper\smile.mp4"复制到"myIOStream\src\e289\111.mp4"(把一个视频文件,从磁盘目录,复制到模块目录)
【思路】
- 根据数据源创建字节输入流对象
- 根据目的地创建字节输出流对象
- 读写数据,实现复制视频
- 释放资源
【要求】
使用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文件,且无法正常观看