高效的文件拷贝之MappedByteBuffer
2016-02-24 16:18
417 查看
我们经常对文件进行操作,但是效率却一般。最近在研究MappedByteBuffer的用法,下面是例子:
void MappedByteBufferTest() {
try {
RandomAccessFile source = new RandomAccessFile("F:\\cmb-flume\\flume\\logs\\relog\\paas0.log", "r");
RandomAccessFile target = new RandomAccessFile("F:\\cmb-flume\\flume\\logs\\relog\\1.log", "rw");
FileChannel in = source.getChannel();
FileChannel out = target.getChannel();
long size = in.size();
MappedByteBuffer mbbi = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
MappedByteBuffer mbbo = out.map(FileChannel.MapMode.READ_WRITE, 0, size);
long start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
byte b = mbbi.get(i);
mbbo.put(i, b);
}
source.close();
target.close();
System.out.println("Spend: " + (System.currentTimeMillis() - start) + "ms");
} catch (Exception e) {
// TODO: handle exception
}
}打印:Spend: 170ms
paas0.log的大小是197M,耗时170毫秒,换算一下1毫秒可以读取1.15M。1秒钟的话可以读取1.13G大小的文件。
void MappedByteBufferTest() {
try {
RandomAccessFile source = new RandomAccessFile("F:\\cmb-flume\\flume\\logs\\relog\\paas0.log", "r");
RandomAccessFile target = new RandomAccessFile("F:\\cmb-flume\\flume\\logs\\relog\\1.log", "rw");
FileChannel in = source.getChannel();
FileChannel out = target.getChannel();
long size = in.size();
MappedByteBuffer mbbi = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
MappedByteBuffer mbbo = out.map(FileChannel.MapMode.READ_WRITE, 0, size);
long start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
byte b = mbbi.get(i);
mbbo.put(i, b);
}
source.close();
target.close();
System.out.println("Spend: " + (System.currentTimeMillis() - start) + "ms");
} catch (Exception e) {
// TODO: handle exception
}
}打印:Spend: 170ms
paas0.log的大小是197M,耗时170毫秒,换算一下1毫秒可以读取1.15M。1秒钟的话可以读取1.13G大小的文件。
相关文章推荐
- Android滑动页面导航效果: PagerSlidingTabStrip
- android 2.3系统闹钟中 重复项的思路
- 从源码编译Android系统的Java类库和JNI动态库的方法
- Android Studio方法前添加自己的注释
- Android事件分发机制完全解析(一)
- iOS App集成Apple Pay教程
- iOS UIView动画详解(Objective-C)
- 实现 UIWebView 的离线缓存 CustionURLCache 库
- android 常用代码github下载地址
- iOS Code Signing的Invalid处理(根证书无效)
- 移动应用通过umeng实现第三方登录--已微信为例
- iOS 编码复习(二)—— runtime
- android apk的编译过程
- Retrofit2.0 使用归纳和 JSONObject Conveter
- QuickApps | 快速启动程序
- 利用源码编译Android系统的APK和可执行命令的方法
- Unity3d iOS 如何添加Admob
- android ndk编写
- iOS开发调试技巧总结
- Xcode7.1与iOS9之坑