Java基础知识强化之IO流笔记30:字节流4种方式复制mp4并测试效率
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并测试效率的更多相关文章
- Java基础知识强化之IO流笔记62:三种方式实现键盘录入
1. 三种方式实现键盘录入 System.in 标准输入流.是从键盘获取数据的 键盘录入数据三种方式: A:main方法的args接收参数. java HelloWorld hello w ...
- Java基础知识强化之IO流笔记03:throws的方式处理异常
1. 什么时候使用throws ? (1)定义功能方法时候,需要把出现的问题暴露出来,让调用者去处理.那么就通过throws在方法上标识. (2)有时候,我们是可以对异常进行处理的,但是又有些时候,我 ...
- Java基础知识强化之IO流笔记17:FileOutputStream构造方法使用
1. 可以参照之前写的笔记: Android(java)学习笔记167:Java中操作文件的类介绍(File + IO流) 2. FileOutputStream(常用的)构造方法: FileOu ...
- Java基础知识强化之IO流笔记71:NIO之 NIO的(New IO流)介绍
1. I/O 简介 I/O ( 输入/输出 ):指的是计算机与外部世界或者一个程序与计算机的其余部分的之间的接口.它对于任何计算机系统都非常关键,因而所有 I/O 的主体实际上是内置在操作系统中的. ...
- Java基础知识强化之IO流笔记68:Properties和IO流集合使用
1. Properties和IO流集合使用 这里的集合必须是Properties集合: public void load(Reader reader):把文件中的数据读取到集合中 public v ...
- Java基础知识强化之IO流笔记66:Properties的概述 和 使用(作为Map集合使用)
1. Properties的概述 Properties:属性集合类.是一个可以和IO流相结合使用的集合类. 该类主要用于读取以项目的配置文件(以.properties结尾的文件 和 xml文件). ...
- Java基础知识强化之IO流笔记44:IO流练习之 复制图片的 4 种方式案例
1. 复制图片的 4 种方式案例: 分析: 复制数据,如果我们知道用记事本打开并能够读懂,就用字符流,否则用字节流. 通过该原理,我们知道我们应该采用字节流. 而字节流有4种方式,所以做这个题目我们有 ...
- Java基础知识强化之IO流笔记43:IO流练习之 复制文本文件的 5 种方式案例
1. 案例分析: 分析: 复制数据,如果我们知道用记事本打开并能够读懂,就用字符流,否则用字节流. 通过该原理,我们知道我们应该采用字符流更方便一些. 而字符流有5种方式,所以做这个题目我们有5种方 ...
- Java基础知识强化之IO流笔记39:字符流缓冲流之复制文本文件案例01
1. 字符流缓冲流之复制文本文件案例 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中 数据源: a.txt -- 读取数据 -- 字符转换流 -- InputStreamRe ...
随机推荐
- 两台CISCO2691测试静态路由汇总,浮动静态路由和负载分担静态路由配置
Dynagen的配置文件内容: #Lab - autostart = False [localhost] [[]] ram = image = C:\Program Files (x86)\Dynam ...
- CISCO的HTTP/HTTPS/SSH配置测试完成
按实验一步一步,倒是很容易的,也理解罗~~ START-CONFIG粗配置文件如下: r1#show run Building configuration... Current configurati ...
- java.math.BigInteger使用心得总结(转)
今天参考课本写了一个关于二进制与十进制转换的程序,程序算法不难,但写完后测试发现不论是二转十还是十转二,对于大于21亿即超过整数范围的数不能很好的转换.都会变成0.参考书籍发现使用使用BigInteg ...
- java 包中的一款经典的singleton模式
/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Versi ...
- ASP.Net4.0中新增23项功能
这篇文章介绍Visual Studio 2010 (ASP.Net 4.0)的新功能. 1.代码片段(Code Snippets): 代码段是预先开发的代码模板,可以节省我们对有关语法思考的时间.在V ...
- SQL Server 2008 R2 制作数据库结构和数据脚本
数据库中包含众多表和数据,有时候需要创建脚本将表结构和数据一起导出 具体方法如下: 1.右键选择数据库,选择“任务”--->“生成脚本” 2.根据需求,选择制作脚本的对象,一般情况选择“表” 3 ...
- FusionCharts 3.2.1 flash 图表展示、数据钻取
StackedColumn3DLineDY.swf 效果展示: 一.页面代码 <div id="chart2div" align="center" sty ...
- the private key for is not installed on this mac
the private key for is not installed on this mac 如果提交 时出现这个问题, 有可能是 keychain重复了, 1:要去keychain中把旧的删除, ...
- (转载)AS3中的mouseEnabled与mouseChildren
(转载)http://www.cnblogs.com/gongchen/archive/2013/05/09/3069055.html mouseEnabled与mouseChildren都是用来确定 ...
- C中位域的使用
一.位域 有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可.为了节省存储空间,并使处理简便,C语言又提供了一 ...