使用NIO与BIO复制文件性能对比
来源:互联网 发布:云计算平台性能指标 编辑:程序博客网 时间:2024/05/01 07:05
package mr.mr03;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;import org.junit.Test;public class CopyFileTest {@Testpublic void testBioCopyFile() throws Exception{long start = System.currentTimeMillis();File fromFile = new File("E:/live_sql/livesql/live-sql.2015-04-24.log");File toFile = new File("E:/live_sql/copy/live-sql.2015-04-24.log");if(!toFile.exists()){toFile.createNewFile();}FileInputStream fis = new FileInputStream(fromFile);FileOutputStream fos = new FileOutputStream(toFile);byte[] buf = new byte[1024 * 1024 * 100];int copyLen = -1 ;while((copyLen = fis.read(buf)) != -1){fos.write(buf, 0, copyLen); }fis.close();fos.close();long end = System.currentTimeMillis();System.out.println("耗时:" + (end - start) + "毫秒"); /** * 文件大小为458M * case1 * 当buf大小设置为 1024(1kb) 耗时:7290毫秒 * case2 * 当buf大小设置为1024*1024*10(10M) 耗时:659毫秒 * case3 * 当buf大小设置为1024*1024*100(100M) 耗时:5252毫秒 */}@Testpublic void testNioCopyFile() throws Exception{long start = System.currentTimeMillis();File fromFile = new File("E:/live_sql/livesql/live-sql.2015-04-24.log");File toFile = new File("E:/live_sql/copy/live-sql.2015-04-24.log");if(!toFile.exists()){toFile.createNewFile();}FileInputStream fis = new FileInputStream(fromFile);FileOutputStream fos = new FileOutputStream(toFile);FileChannel inputChannel = fis.getChannel();FileChannel outputChannel = fos.getChannel();ByteBuffer buf = ByteBuffer.allocateDirect(1024 * 1024) ;int readLen = -1 ;int writeLen = -1 ;while((readLen = inputChannel.read(buf)) != -1){buf.flip();writeLen = outputChannel.write(buf) ;if(writeLen != readLen){System.out.println("writeLen!=readLen," + writeLen + "," + readLen); }buf.clear();}inputChannel.close();outputChannel.close();fis.close();fos.close();long end = System.currentTimeMillis();System.out.println("耗时:" + (end - start) + "毫秒"); /** * 文件大小为458M * case1 * ByteBuffer使用allocate分配1024字节 耗时:2234毫秒 * case2 * ByteBuffer使用allocate分配1024 * 1024 (1M) 耗时:348毫秒 * case3 * ByteBuffer使用allocateDirect分配1024 字节 耗时:2081毫秒 * case4 * ByteBuffer使用allocateDirect分配1024 * 1024 (1M) 耗时:255毫秒 */}}
0 0
- 使用NIO与BIO复制文件性能对比
- java读写文件时nio、bio对比
- tomcat nio与bio技术对比
- tomcat nio与bio压测对比
- Java NIO文件复制以及与传统IO效率对比
- AIO与BIO接口性能对比
- NIO与普通IO文件读写性能对比
- 使用NIO复制文件
- Java BIO、NIO、AIO 对比
- Java NIO与BIO
- NIO 与 BIO 小结
- BIO与NIO、AIO
- NIO与BIO例子
- tomcat bio nio apr 模式性能测试与个人看法
- tomcat bio nio apr 模式性能测试与个人看法
- Tomcat 8(十)HTTP/AJP Connector、Bio/Nio/Apr性能对比
- Tomcat 8(十)HTTP/AJP Connector、Bio/Nio/Apr性能对比
- BIO与NIO的方式实现文件拷贝
- loadNibNamed:方法
- 关于调用系统照相机Activity被销毁问题解决
- 可序列化
- HQL语句学习
- 九章算法面试题40 不用除法求积
- 使用NIO与BIO复制文件性能对比
- 用小米平板,如何看小度路由器所连接的移动硬盘上的视频
- 15年5月嵌入式学习笔记之系统时钟
- Lowest Common Multiple Plus(最小公倍数)
- highchart使用与图像动态代码
- Group by用法
- 提高篇第2-5课第七题
- mybatis批量插入
- AppleWatch开发教程之调试程序使用帮助文档