天天看点

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毫秒
           

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

转载于:https://www.cnblogs.com/dcz1001/p/5927653.html