前面我们共讨论了拷贝文件有三种方式:

1. 第一种,一个字节一个字节的进行拷贝文件操作。

2. 第二种,使用字节数据批量的进行拷贝文件操作。

3. 第三种,使用带缓冲输入输出流来拷贝文件。

那么哪一种性能比较优越呢,也就是耗时时间比较短。测试如下:

package com.dcz.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; public class CopyFileCompare { /**
* 批量的拷贝文件
* @param src
* @param desc
* @throws Exception
*/
public void copyFileByBatch(File srcFile, File destFile) throws Exception{ if(!srcFile.exists()){
throw new IllegalAccessException("文件不存在!");
}
if(!destFile.exists()){
destFile.createNewFile();
} // 创建文件输入流对象
InputStream inputstream = new FileInputStream(srcFile);
// 创建文件输出流对象
OutputStream outputStream = new FileOutputStream(destFile); int b;
byte[] buffer = new byte[10 * 2048];
// 循环读取文件内容到字节序列中,直到读取结束
while((b = inputstream.read(buffer, 0, buffer.length)) != -1){
// 写入一个缓冲字节序列到磁盘中
outputStream.write(buffer);
outputStream.flush();
}
outputStream.close();
inputstream.close(); } /**
* 单字节的方式拷贝文件
* @param srcFile
* @param destFile
* @throws FileNotFoundException
*/
public void copyFileByByte(File srcFile, File destFile) throws Exception { if(!srcFile.exists()){
throw new IllegalAccessException("文件不存在!");
}
if(!destFile.exists()){
destFile.createNewFile();
} // 文件输入流
InputStream fileInputStream = new FileInputStream(srcFile);
// 文件输出流
OutputStream fileOutputStream = new FileOutputStream(destFile); int b = 0;
while((b = fileInputStream.read()) != -1){
fileOutputStream.write(b);
fileOutputStream.flush();
}
fileOutputStream.close();
fileInputStream.close();
} /**
* 拷贝文件带缓冲
* @param srcFile
* @param destFile
* @throws Exception
*/
public void copyFileByBuffer(File srcFile, File destFile)
throws Exception { if(!srcFile.exists()){
throw new IllegalAccessException("文件不存在!");
}
if(!destFile.exists()){
destFile.createNewFile();
} // 缓冲输入流
BufferedInputStream bufferInputStream = new BufferedInputStream(
new FileInputStream(srcFile));
// 缓冲输出流
BufferedOutputStream bufferOutputStream = new BufferedOutputStream(
new FileOutputStream(destFile)); int bytes = 0;
while ((bytes = bufferInputStream.read()) != -1) {
bufferOutputStream.write(bytes);
bufferOutputStream.flush();
}
bufferOutputStream.close();
bufferInputStream.close();
} }

写一个代理类来测试

package com.dcz.io;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy; /**
* CGLIB动态代理
*
* @author DuanCZ
*/ public class CopyFileCompareProxy implements MethodInterceptor { private Enhancer enhance = new Enhancer(); public Object getProxy(Class<?> clazz) {
enhance.setSuperclass(clazz);
enhance.setCallback(this);
return enhance.create();
} @Override
public Object intercept(Object object, Method method, Object[] args,
MethodProxy proxy) throws Throwable { long startTime = System.currentTimeMillis();
proxy.invokeSuper(object, args);
long endTime = System.currentTimeMillis();
System.out.println("拷贝文件 耗时:" + (endTime - startTime) + "毫秒");
return null;
} }

输出结果;

批量拷贝文件 耗时:48毫秒
缓冲拷贝文件 耗时:24132毫秒
字节拷贝文件 耗时:63207毫秒

从上面结果看出,批量拷贝结果是最快的。

Java Io 之拷贝文件性能比较的更多相关文章

  1. Java:IO流与文件基础

    Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...

  2. java io流 对文件夹的操作

    java io流 对文件夹的操作 检查文件夹是否存在 显示文件夹下面的文件 ....更多方法参考 http://www.cnblogs.com/phpyangbo/p/5965781.html ,与文 ...

  3. java io流 创建文件、写入数据、设置输出位置

    java io流 创建文件 写入数据 改变system.out.print的输出位置 //创建文件 //写入数据 //改变system.out.print的输出位置 import java.io.*; ...

  4. 【JAVA】编程(6)--- 应用IO流拷贝文件夹(内含多个文件)到指定位置

    此程序应用了: File 类,及其常用方法: FileInputStream,FileOutputStream类及其常用方法: 递归思维: package com.bjpowernode.javase ...

  5. java IO流 对文件操作的代码集合

    Io流 按照分类 有两种分类 流向方向: 有输入流和输出流 按照操作类型有:字节流和字符流 按照流向方向 字节流的一些操作 //读文件 FileInputStream fis = new FileIn ...

  6. Java IO编程——File文件操作类

    在Java语言里面提供有对于文件操作系统操作的支持,而这个支持就在java.io.File类中进行了定义,也就是说在整个java.io包里面,File类是唯一 一个与文件本身操作(创建.删除.重命名等 ...

  7. java Io流向指定文件输入内容

    package com.hp.io; import java.io.*; public class BufferedWriterTest{ public static void main(String ...

  8. java Io流更新文件内容

    package com.hp.io; import java.io.FileOutputStream; import java.io.IOException; public class FileOut ...

  9. java IO流 Zip文件操作

    一.简介 压缩流操作主要的三个类 ZipOutputStream.ZipFile.ZipInputStream ,经常可以看到各种压缩文件:zip.jar.GZ格式的压缩文件 二.ZipEntry   ...

随机推荐

  1. POJ2528Mayor's posters(离散化 + 线段树)

    题目链接: 题意:给定一些高度都相同的海报去贴,问最后能看见几张海报 The picture below illustrates the case of the sample input. { 8,9 ...

  2. BZOJ1004: [HNOI2008]Cards

    三维01背包算出在每一个置换下不变的染色方案数,Burnside引理计算答案. PS:数据太水所以只算恒等置换也是可以过的. #include<bits/stdc++.h> using n ...

  3. 【原】redux异步操作学习笔记

    摘要: 发觉在学习react的生态链中,react+react-router+webpack+es6+fetch等等这些都基本搞懂的差不多了,可以应用到实战当中,唯独这个redux还不能,学习redu ...

  4. css001 Css需要的html

    Css需要的html 可以忘却的html属性和标签 1.不要用<font>来控制字体的大小和类型.(那要用什么?见第六章) 2.不要用<b>和<i>(b和i只是把字 ...

  5. Oracle开发常用函数

    max 最大数 自动加 1 create or replace function fun_getmaxlot( vend in varchar2 , domain IN VARCHAR2, tag i ...

  6. JQuery------判断拥有某个class或id的div是否存在

    if ($(".Btn,#Show").length > 0) { alert("存在"); } else { alert("不存在" ...

  7. 天行API服务器地址申请

    http://www.tianapi.com/ http://www.huceo.com/post/383.html

  8. mysql查询区分大小写

    Mysql默认查询是不分大小写的,可以在SQL语句中加入 binary来区分大小写: BINARY不是函数,是类型转换运算符,它用来强制它后面的字符串为一个二进制字符串,可以理解为在字符串比较的时候区 ...

  9. linux手动或者自动启动oracle11g的服务 Oracle 自动启动脚本

    手动启动: [oracle@localhost ~]$ sqlplus SQL*Plus: Release 11.2.0.1.0 Production on Wed Mar 26 23:39:52 2 ...

  10. GC算法

    http://www.brpreiss.com/books/opus5/html/page424.html http://www.brpreiss.com/books/opus5/html/page4 ...