在項(xiàng)目中發(fā)現(xiàn)同一個(gè)文件(約500M),在瀏覽器和別的下載工具上使用http協(xié)議下載速度能達(dá)5M+KB/s,而我們的app中只能到6、700+KB/s。一開(kāi)始以為是沒(méi)有使用多線程下載,或者某邏輯代碼是耗時(shí)操作,經(jīng)過(guò)多次對(duì)比實(shí)驗(yàn)和調(diào)試,發(fā)現(xiàn)將下載時(shí)寫(xiě)文件用的RandomAccessFile
的構(gòu)造函數(shù)中的參數(shù)mode
由rwd
改為rw
即可。
RandomAccessFile
的mode
官方文檔:
| 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. |