测试了一下,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. Android Studio & ADT 快捷键配置文件所在目录,自定义后可导入导
  2. Android实现下载文件功能的方法
  3. 【Android】文件读写操作(含SDCard的读写)
  4. android手机客户端上传文件,java servlet服务器端接收并保存到服
  5. exp: 修改Android中strings.xml文件, 动态改变数据
  6. Android读取Excel文件
  7. android中json文件的写法

随机推荐

  1. Android(安卓)任务栈
  2. Android工作总结之如何做一个优秀的Media
  3. Android 平台上玩DOS游戏
  4. android PreferenceActivity
  5. android 开发中的Log
  6. 极光推送(守护进程)
  7. Android 下拉加载更多 上拉刷新 框架 (太
  8. ANDROID L——Material Design综合应用(De
  9. android我的官方文档--No.1 Introduction
  10. Android判断手机中的应用是否具有某些权