java文件复制速度_java中文件复制得速度测试

//需要将apache开发的两个插件包拷到lib目录下:commons-fileupload-1.2.2.jar  commons-io-2.0.1.jar package com.nay.servlet; import java.io.File; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import

最近,对JAVA中文件复制得方式进行速度测试,得出如下结论,测试方案为:

1.传统IO包得复制方法:

void fileByteCopy(String inFile,String outFile) throw* **ception

{

long t1=System.currentTimeMillis();

FileInputStream  in =new FileInputStream(new File(inFile));

FileOutputStream out=new FileOutputStream(new File(outFile),true);

byte[] bytes=new byte[1024];

int i;

while((i=in.read(bytes))!=-1)

{

out.write(bytes,0,i);

}

//out.close();

in.close();

out.close();

System.out.println("花费时间"+(System.currentTimeMillis()-t1)+"豪秒");

}

2.新IO包中基于channel和direct buffer得测试:

void fileBufferCopy(String inFile,String outFile) throw* **ception

{

long t1=System.currentTimeMillis();

//FileInputStream  in =new FileInputStream(new File(inFile));

//FileOutputStream out=new FileOutputStream(new File(outFile));

FileChannel inChannel=new FileInputStream(new File(inFile)).getChannel();

FileChannel ouChannel=new FileOutputStream(new File(outFile)).getChannel();

ByteBuffer buffer=ByteBuffer.allocateDirect(1024);

//while((int i=inChannel.read()))

int i=0;

while(true)

{

buffer.clear();

if((i=inChannel.read(buffer))==-1)

break;

buffer.flip();

ouChannel.write(buffer);

}

//out.close();

inChannel.close();

ouChannel.close();

System.out.println("花费时间"+(System.currentTimeMillis()-t1)+"毫秒");

}

3.直接影射,MappedByteBuffer

void byteMapFileCopy(String inFile,String outFile) throw* **ception

long t1=System.currentTimeMillis();

File file=new File(inFile);

FileChannel out=new FileOutputStream(new File(outFile)).getChannel();

//FileInputStream input=new FileInputStream(file);

MappedByteBuffer buffer=new FileInputStream(file).getChannel().map(FileChannel.MapMode.READ_ONLY,0,file.length());

buffer.load();

//Charset charset=Charset.defaultCharset();

//Charset charset=Charset.forName("GBK");

//CharBuffer charBuffer=charset.decode(buffer);

//System.out.println(charBuffer);

out.write(buffer);

buffer=null;

out.close();

System.out.println("花费时间"+(System.currentTimeMillis()-t1)+"测试");

-------------------------------------------------------------------------------

测试文件大小为:200M,同一windows xp系统在只运行此应用程序(系统程序等必须除外,忙碌情况相同),得到如下近似结果:

一:传统IO复制大致消耗时间:9500-15000毫秒

二:direct buffer方式复制消耗时间:9000-15000毫秒

三:系统影射方式复制大致消耗时间:6000-10000毫秒

从 以上可以看出,单元测试类不能取名为Test 否则即使导入JUnit的包@Test也不好使 #Junit——简介# xUnit是一套基于测试驱动开发的测试框架 JUnit是xUnit的一套子集,xUnit的子集还有pythonUnit、cppUnit JUnit3:不支持注解,必须继承junit.framework.TestCase这个类,且命名必第三种方式稍好,不过从后两种方法得原理可以看出,他们比较依赖系统,因此在系统性能良好得情况下应该要优于传统得IO复制方式,如果复制 得文件大小在20M-200M之间,我会优先选用第三种方案,不过它会直接把整个文件影射进内存,如果文件非常大得话,不知道此种方案能行通否?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值