1.读取文件
File finfo = new File("D://file.txt");
if (finfo.exists() && finfo.length() > 0) {
FileInputStream fis = new FileInputStream(finfo);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String lastPosition = br.readLine();
// 这里计算出来的就是表示的上次该线程下载了多少个字节总数
lastDownloadTotalSize = Integer.parseInt(lastPosition) - startPosition;
startPosition = Integer.parseInt(lastPosition);
fis.close();
2.RandomAccessFile写入文件
2.1
RandomAccessFile inforaf = new RandomAccessFile("D://file.txt", "rwd");
inforaf.write(String.valueOf(startPosition + total).getBytes());
inforaf.close();
2.2
InputStream is = conn.getInputStream();
RandomAccessFile raf = new RandomAccessFile(getDownloadFileName(path), "rw");
raf.seek(startPosition);
int len = 0;
byte[] buffer = new byte[1024];
int total = 0; // downloaded data of current thread in this times;
while ((len = is.read(buffer)) != -1) {
raf.write(buffer, 0, len);
total += len;
RandomAccessFile inforaf = new RandomAccessFile(TOTAL_THREAD_COUNT + getDownloadFileName(path) + threadId + ".txt", "rwd");
inforaf.write(String.valueOf(startPosition + total).getBytes());
inforaf.close();
}