Java Io 之拷贝文件性能比较
前面我们共讨论了拷贝文件有三种方式:
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 之拷贝文件性能比较的更多相关文章
- Java:IO流与文件基础
Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...
- java io流 对文件夹的操作
java io流 对文件夹的操作 检查文件夹是否存在 显示文件夹下面的文件 ....更多方法参考 http://www.cnblogs.com/phpyangbo/p/5965781.html ,与文 ...
- java io流 创建文件、写入数据、设置输出位置
java io流 创建文件 写入数据 改变system.out.print的输出位置 //创建文件 //写入数据 //改变system.out.print的输出位置 import java.io.*; ...
- 【JAVA】编程(6)--- 应用IO流拷贝文件夹(内含多个文件)到指定位置
此程序应用了: File 类,及其常用方法: FileInputStream,FileOutputStream类及其常用方法: 递归思维: package com.bjpowernode.javase ...
- java IO流 对文件操作的代码集合
Io流 按照分类 有两种分类 流向方向: 有输入流和输出流 按照操作类型有:字节流和字符流 按照流向方向 字节流的一些操作 //读文件 FileInputStream fis = new FileIn ...
- Java IO编程——File文件操作类
在Java语言里面提供有对于文件操作系统操作的支持,而这个支持就在java.io.File类中进行了定义,也就是说在整个java.io包里面,File类是唯一 一个与文件本身操作(创建.删除.重命名等 ...
- java Io流向指定文件输入内容
package com.hp.io; import java.io.*; public class BufferedWriterTest{ public static void main(String ...
- java Io流更新文件内容
package com.hp.io; import java.io.FileOutputStream; import java.io.IOException; public class FileOut ...
- java IO流 Zip文件操作
一.简介 压缩流操作主要的三个类 ZipOutputStream.ZipFile.ZipInputStream ,经常可以看到各种压缩文件:zip.jar.GZ格式的压缩文件 二.ZipEntry ...
随机推荐
- DNS(企业级)
构建DNS(企业级) 1.硬件选型 CPU:12C以上配置 内存:16G 网络:千兆 2.初始化系统配置 关闭 iptables service iptables stop chkconfig ipt ...
- python requests
快速上手http://docs.python-requests.org/zh_CN/latest/user/quickstart.html Requests 是使用 Apache2 Licensed ...
- php爬虫 phpspider
<?php /** * Created by PhpStorm. * User: brady * Date: 2016/12/9 * Time: 17:32 */ ini_set("m ...
- SpringMVC处理请求流程
SpringMVC核心处理流程: 1.DispatcherServlet前端控制器接收发过来的请求,交给HandlerMapping处理器映射器 2.HandlerMapping处理器映射器,根据请求 ...
- 使用RawSocket进行网络抓包
aw socket,即原始套接字,可以接收本机网卡上的数据帧或者数据包,对与监听网络的流量和分析是很有作用的,一共可以有3种方式创建这种socket. 中文名 原始套接字 外文名 RAW SOCKET ...
- ansible执行playbook时间显示的python脚本
import datetime import os import time from ansible.plugins.callback import CallbackBase class Callba ...
- How to get http response.
public class HttpWebResponseUtility { public static string CreateGetHttpResponse(string url) { var r ...
- How to know if file is complete on the server using FTP
This is a very old and well-known problem. There is no way to be absolutely certain a file being wri ...
- APP抓链接工具(Fiddler版)
1.下载Fiddler源代码: http://pan.baidu.com/s/1hqEUK0O 2.修改如下源代码: 3.运行截图:
- 浅谈JavaScript中的Ajax
引言 作为一名WEB开发者,我想Ajax技术是一定需要掌握的.你也许平时没有使用JavaScript真正的写过Ajax.但是你一定使用过JQuery里面的相关函数来进行异步调用.今天我们就来介绍下原生 ...