前面我們共討論了拷貝文件有三種方式:
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毫秒
從上面結果看出,批量拷貝結果是最快的。