1 package day9.lesson1;
2
3 import java.io.*;
4
5 /*
6 1.2 案例-字节流复制视频
7
8 根据数据源创建字节输入流对象
9 根据目的地创建字节输出流对象
10 读写数据,复制视频
11 释放资源
12
13 四种方式实现赋值视频,记录每种方式的用时
14 1 基本字节流一次读写一个字节
15 2 基本字节流一次读写一个字节数组
16 3 字节缓冲流一次读写一个字节
17 4 字节缓冲流一次读写一个字节数组
18 */
19 public class CopyAviDemo {
20 public static void main(String[] args) throws IOException{
21 long startTime = System.currentTimeMillis();
22
23 // method1(); //耗时:4116毫秒
24 // method2(); //耗时:8毫秒
25 // method3(); //耗时:38毫秒
26 method4(); //耗时:3毫秒 证实字节缓冲流可以帮助字节流提高效率
27
28 long endTime = System.currentTimeMillis();
29 System.out.println("耗时:" + (endTime-startTime) + "毫秒");
30 }
31
32 // 基本字节流一次读写一个字节
33 public static void method1() throws IOException {
34 FileInputStream fis = new FileInputStream("stage2\\src\\day9\\lesson1\\1.avi");
35 FileOutputStream fos = new FileOutputStream("stage2\\src\\day9\\lesson1\\copy\\1.avi");
36
37 int by;
38 while ((by=fis.read()) != -1){
39 fos.write(by);
40 }
41
42 fos.close();
43 fis.close();
44 }
45
46 // 基本字节流一次读写一个字节数组
47 public static void method2() throws IOException{
48 FileInputStream fis = new FileInputStream("stage2\\src\\day9\\lesson1\\1.avi");
49 FileOutputStream fos = new FileOutputStream("stage2\\src\\day9\\lesson1\\copy\\1.avi");
50
51 byte[] bys = new byte[1024];
52 int len;
53 while ((len=fis.read(bys)) != -1){
54 fos.write(bys, 0, len);
55 }
56
57 fos.close();
58 fis.close();
59 }
60
61 // 字节缓冲流一次读写一个字节
62 public static void method3() throws IOException{
63 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("stage2\\src\\day9\\lesson1\\1.avi"));
64 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("stage2\\src\\day9\\lesson1\\copy\\1.avi"));
65
66 int by;
67 while ((by=bis.read()) != -1){
68 bos.write(by);
69 }
70
71 bos.close();
72 bis.close();
73 }
74
75 // 字节缓冲流一次读写一个字节数组
76 public static void method4() throws IOException{
77 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("stage2\\src\\day9\\lesson1\\1.avi"));
78 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("stage2\\src\\day9\\lesson1\\copy\\1.avi"));
79
80 byte[] bys = new byte[1024];
81 int len;
82 while ((len=bis.read(bys)) != -1){
83 bos.write(bys, 0, len);
84 }
85
86 bos.close();
87 bis.close();
88 }
89
90 }