Java基础知识强化之IO流笔记30:字节流4种方式复制mp4并测试效率

1. 需求:把e:\\哥有老婆.mp4 复制到当前项目目录下的copy.mp4中

字节流四种方式复制文件:
  • 基本字节流一次读写一个字节
  • 基本字节流一次读写一个字节数组
  • 高效字节流一次读写一个字节
  • 高效字节流一次读写一个字节数组

2. 代码示例:

 1 package cn.itcast_06;
 2
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedOutputStream;
 5 import java.io.FileInputStream;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8
 9 /*
10  * 需求:把e:\\哥有老婆.mp4复制到当前项目目录下的copy.mp4中
11  *
12  * 字节流四种方式复制文件:
13  * 基本字节流一次读写一个字节:      共耗时:117235 毫秒
14  * 基本字节流一次读写一个字节数组:  共耗时:156 毫秒
15  * 高效字节流一次读写一个字节:      共耗时:1141 毫秒
16  * 高效字节流一次读写一个字节数组:  共耗时:47 毫秒
17  */
18 public class CopyMp4Demo {
19     public static void main(String[] args) throws IOException {
20         long start = System.currentTimeMillis();
21         // method1("e:\\哥有老婆.mp4", "copy1.mp4");
22         // method2("e:\\哥有老婆.mp4", "copy2.mp4");
23         // method3("e:\\哥有老婆.mp4", "copy3.mp4");
24         method4("e:\\哥有老婆.mp4", "copy4.mp4");
25         long end = System.currentTimeMillis();
26         System.out.println("共耗时:" + (end - start) + "毫秒");
27     }
28
29     // 高效字节流一次读写一个字节数组:
30     public static void method4(String srcString, String destString)
31             throws IOException {
32         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
33                 srcString));
34         BufferedOutputStream bos = new BufferedOutputStream(
35                 new FileOutputStream(destString));
36
37         byte[] bys = new byte[1024];
38         int len = 0;
39         while ((len = bis.read(bys)) != -1) {
40             bos.write(bys, 0, len);
41         }
42
43         bos.close();
44         bis.close();
45     }
46
47     // 高效字节流一次读写一个字节:
48     public static void method3(String srcString, String destString)
49             throws IOException {
50         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
51                 srcString));
52         BufferedOutputStream bos = new BufferedOutputStream(
53                 new FileOutputStream(destString));
54
55         int by = 0;
56         while ((by = bis.read()) != -1) {
57             bos.write(by);
58
59         }
60
61         bos.close();
62         bis.close();
63     }
64
65     // 基本字节流一次读写一个字节数组
66     public static void method2(String srcString, String destString)
67             throws IOException {
68         FileInputStream fis = new FileInputStream(srcString);
69         FileOutputStream fos = new FileOutputStream(destString);
70
71         byte[] bys = new byte[1024];
72         int len = 0;
73         while ((len = fis.read(bys)) != -1) {
74             fos.write(bys, 0, len);
75         }
76
77         fos.close();
78         fis.close();
79     }
80
81     // 基本字节流一次读写一个字节
82     public static void method1(String srcString, String destString)
83             throws IOException {
84         FileInputStream fis = new FileInputStream(srcString);
85         FileOutputStream fos = new FileOutputStream(destString);
86
87         int by = 0;
88         while ((by = fis.read()) != -1) {
89             fos.write(by);
90         }
91
92         fos.close();
93         fis.close();
94     }
95 }
时间: 2024-08-06 16:06:41

Java基础知识强化之IO流笔记30:字节流4种方式复制mp4并测试效率的相关文章

Java基础知识强化之IO流笔记62:三种方式实现键盘录入

1. 三种方式实现键盘录入     System.in 标准输入流.是从键盘获取数据的 键盘录入数据三种方式:  A:main方法的args接收参数.  java HelloWorld hello world java  B:Scanner(JDK5以后的)  Scanner sc = new Scanner(System.in);  String s = sc.nextLine();  int x = sc.nextInt()  C:通过字符缓冲流包装标准输入流实现  BufferedRead

Java基础知识强化之IO流笔记17:FileOutputStream构造方法使用

1. 可以参照之前写的笔记:   Android(java)学习笔记167:Java中操作文件的类介绍(File + IO流) 2. FileOutputStream(常用的)构造方法: FileOutputStream(File file)           Constructs a new FileOutputStream on the File file. FileOutputStream(String filename)           Constructs a new FileO

Java基础知识强化之IO流笔记35:InputStreamReader/OutputStreamWriter 复制文本文件案例01

1. 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中. 数据源:  a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader 目的地:  b.txt -- 写出数据 -- 字符转换流 -- OutputStreamWriter InputStreamReader/OutputStreamWriter不仅是转换流(将字节流转换为字符流),也是字符流Reader/Writer的具体实现子类. 2.代码示例: 1 package cn.itcas

Java基础知识强化之IO流笔记16:IO流的概述和分类

1. IO流的分类   流向:     (1)输入流:读取数据到内存     (2)输出流:写入数据到硬盘(磁盘)   操作的数据类型:    (1)字节流:操作的数据是字节                            输入流                            输出流        (2)字符流:操作的数据是字符,为了方便操作文本数据,Java就提供了字符流.                            输入流                      

Java基础知识强化之IO流笔记66:Properties的概述 和 使用(作为Map集合使用)

1. Properties的概述  Properties:属性集合类.是一个可以和IO流相结合使用的集合类.Properties 可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串. Properties是Hashtable的子类,说明是一个Map集合. 2. Properties作为Map集合使用 1 package cn.itcast_08; 2 3 import java.util.Properties; 4 import java.util.Set; 5 6 /* 7 *

Java基础知识强化之IO流笔记44:IO流练习之 复制图片的 4 种方式案例

1. 复制图片的 4 种方式案例: 分析: 复制数据,如果我们知道用记事本打开并能够读懂,就用字符流,否则用字节流. 通过该原理,我们知道我们应该采用字节流. 而字节流有4种方式,所以做这个题目我们有4种方式.推荐掌握第4种. 数据源: c:\\a.jpg -- FileInputStream -- BufferedInputStream 目的地: d:\\b.jpg -- FileOutputStream -- BufferedOutputStream 2. 4 种方式代码示例: 1 pack

Java基础知识强化之IO流笔记61:标准输入输出流的本质

1. 标准输入输出流  System类中的两个成员变量: public static final InputStream in :"标准"输入流. public static final PrintStream out :"标准"输出流. 备注: InputStream is = System.in; PrintStream ps = System.out; 代码示例: 1 package cn.itcast_04; 2 3 import java.io.Print

Java基础知识强化之IO流笔记22:FileInputStream / FileOutputStream 复制文本文件案例

1. 使用字节流FileInputStream / FileOutputStream 复制文本文件案例: 分析: (1)数据源:从哪里来 a.txt   --   读取数据  --  FileInputStream (2)目的地:到哪里去 b.txt   --   写数据    --   FileOutputStream 2. 代码示例: 1 package cn.itcast_03; 2 3 import java.io.FileInputStream; 4 import java.io.Fi

Java基础知识强化之IO流笔记68:Properties和IO流集合使用

1. Properties和IO流集合使用 这里的集合必须是Properties集合:  public void load(Reader reader):把文件中的数据读取到集合中  public void store(Writer writer,String comments):把集合中的数据存储到文件 2. 代码实现: 1 package cn.itcast_08; 2 3 import java.io.FileReader; 4 import java.io.FileWriter; 5 i