java文件复制速度_java中io流的复制方式和速度比较

gistfile1.txt

/*

* 进行文件复制方法 及效率对比

* 四种方式

* 1. 字节流读写单个字节 135095

* 2. 字节流读写字节数组 208 推荐

* 3. 缓冲区读写单个字节 1187

* 4. 缓冲区读写字节数组 90 强烈推荐

* 我们复制一个25M大小的视屏文件1.avi

* 数据源: c:\\1.avi

* 数据目的: d:\\1.avi

*

* 自己的复制功能,比固体要快,比Windows也要快

* 为什么:

* 流底层也是找Windows读写

* 固态中,复制文件,依赖Windows

* 复制,实现很多功能:

* 目的,够不够装文件

* 判断用户有没有复制权限,粘贴权限

*

*/

import java.io.*;

public class CopyFile_Test {

public static void main(String[] args) {

// 测试效率,采用毫秒值计算

long start = System.currentTimeMillis();

copy_4();

long end = System.currentTimeMillis();

System.out.println(end - start);

}

/*

* 4. 缓冲区读写字节数组

*/

public static void copy_4(){

// 创建2个缓冲区流的变量,=null

BufferedInputStream bis = null;

BufferedOutputStream bos = null;

try {

// 创建字节流缓冲区对象,传递字节流

bis = new BufferedInputStream(

new FileInputStream("c:\\m.exe"));

// 创建字节输出流缓冲区对象,传递字节输出流

bos = new BufferedOutputStream(

new FileOutputStream("d:\\m.exe"));

// 读写字节数组

/*

*当数组长度,在一定范围内长度更大的时候,复制速度会更快,

*但如果过长,系统在为bytes分配数组的时间就会很长,反而降低了复 *制速度.所以不能使用太长的数组.

*/

byte[] bytes = new byte[1024];

int len = 0;

while ((len = bis.read(bytes)) != -1) {

bos.write(bytes,0,len);

}

} catch (IOException ex) {

throw new RuntimeException("文件复制失败");

} finally {

MyCloseStream.close(bis, bos);

}

}

/*

* 3. 缓冲区读写单个字节

*/

public static void copy_3() {

// 创建2个缓冲区流的变量,=null

BufferedInputStream bis = null;

BufferedOutputStream bos = null;

try {

// 创建字节流缓冲区对象,传递字节流

bis = new BufferedInputStream(

new FileInputStream("c:\\1.avi"));

// 创建字节输出流缓冲区对象,传递字节输出流

bos = new BufferedOutputStream(

new FileOutputStream("d:\\1.avi"));

// 读写单个字节

int len = 0;

while ((len = bis.read()) != -1) {

bos.write(len);

}

} catch (IOException ex) {

throw new RuntimeException("文件复制失败");

} finally {

MyCloseStream.close(bis, bos);

}

}

/*

* 2. 字节流读写字节数组

*/

public static void copy_2() {

FileInputStream fis = null;

FileOutputStream fos = null;

try {

fis = new FileInputStream("c:\\1.avi");

fos = new FileOutputStream("d:\\1.avi");

// 定义字节数组,长度1024整数倍

byte[] bytes = new byte[1024];

int len = 0;

while ((len = fis.read(bytes)) != -1) {

// fos流写字节数组

fos.write(bytes, 0, len);

}

} catch (IOException ex) {

ex.printStackTrace();

throw new RuntimeException("复制失败");

} finally {

// 调用工具类close方法

MyCloseStream.close(fis, fos);

}

}

/*

* 1. 字节流读写单个字节

*/

public static void copy_1() {

// 声明2个流对象,=null

FileInputStream fis = null;

FileOutputStream fos = null;

try {

// 创建2个流对象,new

fis = new FileInputStream("c:\\1.avi");

fos = new FileOutputStream("d:\\1.avi");

// fis读取1个字节,fos写1个字节

int len = 0;

while ((len = fis.read()) != -1) {

fos.write(len);

}

} catch (IOException ex) {

ex.printStackTrace();

throw new RuntimeException("复制失败");

} finally {

MyCloseStream.close(fis, fos);

}

}

}

MyCloseStream.java

/* 关闭方法close的细节

* 单独的close方法,定义一套自己的try catch处理

* 保证所有的close都会执行到

*/

import java.io.*;

public class CopyFile {

public static void main(String[] args) {

//声明2个流对象,=null

FileInputStream fis = null;

FileOutputStream fos = null;

try{

//创建2个流对象,new

fis = new FileInputStream("c:\\t.jpg");

fos = new FileOutputStream("d:\\t.jpg");

//fis读取1个字节,fos写1个字节

int len = 0 ;

while((len = fis.read())!=-1){

fos.write(len);

}

}catch(IOException ex){

ex.printStackTrace();

throw new RuntimeException("复制失败");

}finally{

try{

if(fos!=null)

fos.close();

}catch(IOException ex){

throw new RuntimeException("释放资源失败");

}finally{

try{

if(fis!=null)

fis.close();

}catch(IOException ex){

throw new RuntimeException("释放资源失败");

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值