测试了一下,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();
} catch (Exception e) {
e.printStackTrace();
}
end_time = System.currentTimeMillis();
Log.d("", "BufferedOutputStream write:" + length +" 耗时:"+(end_time-start_time));
}
测试环境 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));
}