Android RandomAccessFile模式

在项目中发现同一个文件(约500M),在浏览器和别的下载工具上使用http协议下载速度能达5M+KB/s,而我们的app中只能到6、700+KB/s。一开始以为是没有使用多线程下载,或者某逻辑代码是耗时操作,经过多次对比实验和调试,发现将下载时写文件用的RandomAccessFile的构造函数中的参数moderwd改为rw即可。

RandomAccessFilemode官方文档:

Value Meaning
“r” for reading only. Invoking any of the write methods of the resulting object will cause an IOException.
“rw” Open for reading and writing. If the file does not already exist then an attempt will be made to create it.
“rws” Open for reading and writing, as with “rw”, and also require that every update to the file’s content or metadata be written synchronously to the underlying storage device.
“rwd” Open for reading and writing, as with “rw”, and also require that every update to the file’s content be written synchronously to the underlying storage device.

参考: