最近正好遇到了使用RandomAccessFile做斷點下載的情況,被一個問題坑了好多次
本來的代碼:
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rwd");
FileOutputStream fos = new FileOutputStream(file);
這樣兩句聯在一起,有問題!!!
FileOutputStream fos = new FileOutputStream(file);這句會把f.getFilePointer=0以及 f.length()=0。。。導致
RandomAccessFile seek沒起到作用,而且會讓后面生成一個錯誤的文件!!!
要根據情況,在不同的分支里分別new 這兩個對象!