Linux下实现断点续传的原理介绍( 二 )


保存文件采用的方法 。
我采用的是 IO 包中的 RandAccessFile 类 。
操作相当简单,假设从 2000070 处开始保存文件,代码如下:
RandomAccess oSavedFile = new RandomAccessFile(“down.zip”,“rw”);
long nPos = 2000070;
// 定位文件指针到 nPos 位置
oSavedFile.seek(nPos);
byte[] b = new byte[1024];
int nRead;
// 从输入流中读入字节流,然后写到文件中
while((nRead=input.read(b,0,1024)) 》 0)
{
oSavedFile.write(b,0,nRead);
}

推荐阅读