测试了一下,android 下写文件性能:


测试环境 eben T4 android 2.3.4

小文件时RandomAccessFile 比FileOutputStream 快

大文件正好相反FileOutputStream 比 RandomAccessFile 快

我测试

100M , final int length = 1024 * 1024 * 100; 时间ms
RandomAccessFile write: 104857600耗 时:33874
BufferedOutputStream write: 104857600耗 时:17814

50M
, final int length = 1024 * 1024 * 50; 时间ms
RandomAccessFile write:52428800 耗时:16430
BufferedOutputStream write:52428800 耗时:9497

10M
, final int length = 1024 * 1024 * 10; 时间ms
RandomAccessFile write:10485760 耗时:454
BufferedOutputStream write:10485760 耗时:958


建议开发者存储文件时选择适合的方式。



测试代码如下:

private void testFileWrite(){
RandomAccessFile mRandomAccessFile = null;
File randomFile;
randomFile = new File("/sdcard/randomFile.test");

final int length = 1024 * 1024 * 10; // *100 //*50
byte buffer[] = new byte[1024];
for (int i = 0; i < 1024; ++i) {
buffer[i] = (byte) (0xFF & i);
}

long start_time = System.currentTimeMillis();
try {
mRandomAccessFile = new RandomAccessFile(randomFile, "rw");
mRandomAccessFile.setLength(length);
for (int i = 0, max = length / 1024; i < max; ++i){
mRandomAccessFile.write(buffer, 0, 1024);
}
if (mRandomAccessFile != null) {
mRandomAccessFile.close();
}
} catch (IOException e) {
e.printStackTrace();
}
long end_time = System.currentTimeMillis();
Log.d("", "RandomAccessFile write:" + length +" 耗时:"+(end_time-start_time));
start_time = System.currentTimeMillis();
/////////////////////////////////////////////////////////////////////////
FileOutputStream downFileWriter = null;
BufferedOutputStream downFileBuffer = null;
File streamFile = new File("/sdcard/streamFile.test");

try {
downFileWriter = new FileOutputStream(streamFile, true);
downFileBuffer = new BufferedOutputStream(downFileWriter,102400);
for (int i = 0, max = length / 1024; i < max; ++i){
downFileBuffer.write(buffer, 0, 1024);
}

if (null !=downFileBuffer) {
downFileBuffer.flush();
downFileBuffer.close();
downFileBuffer = null;
}
if (null != downFileWriter){
downFileWriter.flush();

downFileWriter.close();

downFileWriter = null;

}
} catch (Exception e) {

e.printStackTrace();
}

end_time = System.currentTimeMillis();
Log.d("", "BufferedOutputStream write:" + length +" 耗时:"+(end_time-start_time));
}

更多相关文章

  1. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. Android(安卓)Studio & ADT 快捷键配置文件所在目录,自定义后可导
  4. Android(安卓)SDK Manager无法更新的解决
  5. 创建android逐帧动画的两种方式
  6. android 单元测试
  7. Android(安卓)Settings
  8. 【Android】文件读写操作(含SDCard的读写)
  9. android 目录结构,adb环境变量配置

随机推荐

  1. 转载 Android(安卓)脚本设计之 SL4A
  2. conversion to dalvik format failed wit
  3. 有关Android(安卓)SQLite编程的一些知识
  4. Mac OS X 下编译android源码
  5. Android命令行打包过程
  6. android 面试题经典
  7. 深入理解Android消息处理系统——Looper
  8. Android学习笔记(8) - GridView与ImageVi
  9. [置顶] android ScrollView滚动距离和判
  10. 在Ubuntu为Android硬件抽象层(HAL)模块编写