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. |

参考:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容