设为首页 加入收藏

TOP

MappedByteBuffer高速缓存文件、RandomAccessFile随机访问(二)
2015-07-20 17:57:57 来源: 作者: 【 】 浏览:3
Tags:MappedByteBuffer 高速 文件 RandomAccessFile 随机 访问
nt i = 0; i < size; i++) { b = mbbi.get(i); mbbo.put(i, b); } fci.close(); fco.close(); rafi.close(); rafo.close(); System.out.println("input: " + (double) (System.currentTimeMillis() - start) / 1000 + "s"); } // 2、自己处理Buffer(RandomAccessFile): 0.13s public static void BufferTest() throws Exception{ String srcFile = "F:\\Ebook\\偷天.txt"; String destFile = "F:\\Ebook\\toutian.txt"; RandomAccessFile rafi = new RandomAccessFile(srcFile, "r"); RandomAccessFile rafo = new RandomAccessFile(destFile, "rw"); byte[] buf = new byte[80]; long start = System.currentTimeMillis(); int c = rafi.read(buf); System.out.println("output: " + (double) (System.currentTimeMillis() - start) / 1000 + "s"); start = System.currentTimeMillis(); while (c > 0) { if (c == buf.length) { rafo.write(buf); } else { rafo.write(buf, 0, c); } c = rafi.read(buf); } System.out.println("input: " + (double) (System.currentTimeMillis() - start) / 1000 + "s"); rafi.close(); rafo.close(); } // 3、BufferedInputStream&BufferedOutputStream: 3.02s public static void BufferedInputStreamTest() throws Exception{ String srcFile = "F:\\Ebook\\偷天.txt"; String destFile = "F:\\Ebook\\toutian.txt"; FileInputStream rafi = new FileInputStream(srcFile); FileOutputStream rafo = new FileOutputStream(destFile); BufferedInputStream bis = new BufferedInputStream(rafi, 8192); BufferedOutputStream bos = new BufferedOutputStream(rafo, 8192); long size = rafi.available(); long start = System.currentTimeMillis(); for (int i = 0; i < size; i++) { byte b = (byte) bis.read(); bos.write(b); } rafi.close(); rafo.close(); System.out.println("time: " + (double) (System.currentTimeMillis() - start) / 1000 + "s"); } }


总结:

1、RandomAccessFile是Java输入输出流体系中功能最丰富的文件内容访问类,他提供 了众多的方法来访问文件,它既可以读取文件的内容,也可以说向文件输出数据,本身不带缓冲读写,和FileInputStream、FileOutputStream等一样,直接按字节读写时,性能不可接受;


2、使用MappedByteBuffer读写,固然性能会得到极大提升;其实只要自己处理缓冲,性能都会有非常大的提升,比如以下两种方式中第一种使用了MappedByteBuffer,第二种自己进行缓冲处理后,对于几兆的文件,后者的效率甚至高于前者,可以从几个size大小看出运行速度,当size较大的时候一次性的读取速度是慢些,但是整体的效率非常之高。


3、BufferedXXXX之类的缓冲流,如果仅使用默认的buffer size,性能不一定最优,要权衡不同情况各种因素设置大小。



首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ 3616 Milking Time DP题解 下一篇HDU 1754 I Hate it (线段树最大..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: