1. 需求:把f:\\Jick.mp4 复制到当前项目目录下的copy.mp4中

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

2. 代码示例:

 package com.himi.iocopy;

 import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; /*
* 需求:把f:\\Jick.mp4复制到当前项目目录下的copy.mp4中
*
* 字节流四种方式复制文件:
* 基本字节流一次读写一个字节(method1): 共耗时:138872 毫秒
* 基本字节流一次读写一个字节数组(method2): 共耗时:160 毫秒
* 高效字节流一次读写一个字节(method3): 共耗时:643 毫秒
* 高效字节流一次读写一个字节数组(method4): 共耗时:40 毫秒
*/
public class IOCopy {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis(); //method1("f:\\Jick.mp4", "copy1.mp4");
//method2("f:\\Jick.mp4", "copy2.mp4");
//method3("f:\\Jick.mp4", "copy3.mp4");
method4("f:\\Jick.mp4", "copy4.mp4");
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒"); } // 高效字节流一次读写一个字节数组:
public static void method4(String srcString, String destString)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcString));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destString)); byte[] bys = new byte[];
int len = ;
while ((len = bis.read(bys)) != -) {
bos.write(bys, , len);
} bos.close();
bis.close();
} // 高效字节流一次读写一个字节:
public static void method3(String srcString, String destString)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcString));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destString)); int by = ;
while ((by = bis.read()) != -) {
bos.write(by); } bos.close();
bis.close();
} // 基本字节流一次读写一个字节数组
public static void method2(String srcString, String destString)
throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString); byte[] bys = new byte[];
int len = ;
while ((len = fis.read(bys)) != -) {
fos.write(bys, , len);
} fos.close();
fis.close();
} // 基本字节流一次读写一个字节
public static void method1(String srcString, String destString)
throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString); int by = ;
while ((by = fis.read()) != -) {
fos.write(by);
} fos.close();
fis.close();
} }

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

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

    1. 三种方式实现键盘录入     System.in 标准输入流.是从键盘获取数据的 键盘录入数据三种方式:  A:main方法的args接收参数.  java HelloWorld hello w ...

  2. Java基础知识强化之IO流笔记03:throws的方式处理异常

    1. 什么时候使用throws ? (1)定义功能方法时候,需要把出现的问题暴露出来,让调用者去处理.那么就通过throws在方法上标识. (2)有时候,我们是可以对异常进行处理的,但是又有些时候,我 ...

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

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

  4. Java基础知识强化之IO流笔记71:NIO之 NIO的(New IO流)介绍

    1. I/O 简介 I/O ( 输入/输出  ):指的是计算机与外部世界或者一个程序与计算机的其余部分的之间的接口.它对于任何计算机系统都非常关键,因而所有 I/O 的主体实际上是内置在操作系统中的. ...

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

    1. Properties和IO流集合使用 这里的集合必须是Properties集合:  public void load(Reader reader):把文件中的数据读取到集合中  public v ...

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

    1. Properties的概述  Properties:属性集合类.是一个可以和IO流相结合使用的集合类. 该类主要用于读取以项目的配置文件(以.properties结尾的文件 和 xml文件). ...

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

    1. 复制图片的 4 种方式案例: 分析: 复制数据,如果我们知道用记事本打开并能够读懂,就用字符流,否则用字节流. 通过该原理,我们知道我们应该采用字节流. 而字节流有4种方式,所以做这个题目我们有 ...

  8. Java基础知识强化之IO流笔记43:IO流练习之 复制文本文件的 5 种方式案例

     1. 案例分析: 分析: 复制数据,如果我们知道用记事本打开并能够读懂,就用字符流,否则用字节流. 通过该原理,我们知道我们应该采用字符流更方便一些. 而字符流有5种方式,所以做这个题目我们有5种方 ...

  9. Java基础知识强化之IO流笔记39:字符流缓冲流之复制文本文件案例01

    1. 字符流缓冲流之复制文本文件案例 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中 数据源: a.txt -- 读取数据 -- 字符转换流 -- InputStreamRe ...

随机推荐

  1. ECshop 在迁移到 PHP7 时遇到的兼容性问题

    在 PHP7 上安装 ECShop V2.7.3时,报错! Deprecated: Methods with the same name as their class will not be cons ...

  2. 李洪强iOS开发Swift篇—01_简单介绍

    李洪强iOS开发Swift篇—01_简单介绍 一.简介 Swift是苹果于2014年WWDC(苹果开发者大会)发布的全新编程语言 Swift在天朝译为“雨燕”,是它的LOGO 是一只燕子,跟Objec ...

  3. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除bean自动装配的歧义@Primary

    一. 假设有如下三个类实现同一个接口,则自动装配时会产生歧义 @Component public class Cake implements Dessert { ... } @Component pu ...

  4. linux使用man命令后退出

    linux使用man命令后 使用q退出

  5. POJ_2229_Sumsets_(动态规划)

    描述 http://poj.org/problem?id=2229 将一个数n分解为2的幂之和共有几种分法? Sumsets Time Limit: 2000MS   Memory Limit: 20 ...

  6. (转载)c库不正确问题

    (转载)http://blog.csdn.net/piratejk/article/details/6115748 在linux下面变成,有时候在一个发行版本上编译通过,并且可以运行,但是将程序拷贝到 ...

  7. Interleaving String——Leetcode

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  8. MVC Model 数据注解与验证

    常用验证特性: using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Sch ...

  9. Poj 3695-Rectangles 矩形切割

    Rectangles Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3846   Accepted: 1124 Descri ...

  10. C语言练习题_邮票组合

    背景:        我们寄信都要贴邮票,在邮局有一些小面值的邮票,通过这些小面值邮票中的一张或几张的组合,可以满足不同邮件的不同的邮资.        现在,邮局有4种不同面值的邮票.在每个信封上最 ...