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

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. 回调函数通俗解析(之前看了很久都不理解,今天终于ok啦)

    自学jquery的时候,看到一英文词(Callback),顿时背部隐隐冒冷汗.迅速google之,发现原来中文翻译成回调.也就是回调函数了.不懂啊,于是在google回调函数,发现网上的中文解释实在是 ...

  2. R in bioinformatic

    TCGA https://www.bioconductor.org/packages/release/bioc/vignettes/TCGAbiolinks/inst/doc/tcgaBiolinks ...

  3. Linux 常用命令笔记 (持续更新)

    声明:本文是转载前辈的,地址:http://www.cnblogs.com/tovep/articles/2473147.html 在tomcat的bin目录下执行 ./shutdown.sh 为了查 ...

  4. context.Request.Files为NULL问题 在实现图片上传功能的时候出现在ashx等处理页面出现context.Request.Files为NULL异常,有几点需要注意:

    .在客户端可以将form用submit提交,如下: <%@ Page Language="C#" AutoEventWireup="true" CodeF ...

  5. vs------密钥

    HM6NR-QXX7C-DFW2Y-8B82K-WTYJV

  6. sudo 出现unable to resolve host 解决方法

    inux 环境, 假设这台机器名字叫dev(机器的hostname), 每次执行sudo 就出现这个警告讯息:sudo: unable to resolve host dev虽然sudo 还是可以正常 ...

  7. SQL日期格式转换

    CONVERT(nvarchar(20), [Date],101) as 'Date'    10/20/2016 CONVERT(nvarchar(20), [Date],102) as 'Date ...

  8. DllImport dll中有些啥函数 及 dll中是否用到了别的dll

    在加载dll的时候不知道dll中有哪些接口怎么办,或者使用别人封装的东西时报出类似于“无法在 DLL“XXX.dll”中找到名为“XXX函数”的入口点.”     1.通过LordPE这个软件来看dl ...

  9. JavaWeb学习笔记——Web开发模式:Mode I和Mode II

  10. Order Independent Transparency

    http://on-demand.gputechconf.com/gtc/2014/presentations/S4385-order-independent-transparency-opengl. ...