java文件 续传_java文件续传

packagecom.yangtao.test;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.RandomAccessFile;importjava.nio.channels.FileChannel;publicclassTestFile {privatestaticintSIZE=1024*1024*2;publicvoidmove(File srcFile, File destFile) {try{intlength=SIZE;

FileInputStream in=newFileInputStream(srcFile);

FileOutputStream out=newFileOutputStream(destFile);

FileChannel inC=in.getChannel();

FileChannel outC=out.getChannel();while(true) {if(inC.position()==inC.size()) {

in.close();

out.close();

inC.close();

outC.close();break;

}if((inC.size()-inC.position())

length=(int) (inC.size()-inC.position());//taskManager.changeoffset(id, 100);}else{

length=SIZE*10;

}

inC.transferTo(inC.position(), length, outC);

inC.position(inC.position()+length);

}

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

}publicvoidrunUnFinished(File srcFile, File destFile) {if(destFile.exists()) {try{

RandomAccessFile randomSrcFile=newRandomAccessFile(srcFile,"rw");longdestFileSize=destFile.length();

RandomAccessFile randomDestFile=newRandomAccessFile(destFile,"rw");

randomSrcFile.seek(destFileSize-1024*2);

randomDestFile.seek(destFileSize-1024*2);

FileChannel fin=randomSrcFile.getChannel();

FileChannel fout=randomDestFile.getChannel();//fin.position(destFileSize);while(true) {intlength=SIZE;if(fin.position()==fin.size()) {

fin.close();

fout.close();break;

}if((fin.size()-fin.position())

length=(int) (fin.size()-fin.position());//taskManager.changeoffset(id, 100);}else{

length=SIZE*10;

}

fin.transferTo(fin.position(), length, fout);

fin.position(fin.position()+length);

}

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {//TODO Auto-generated catch blocke.printStackTrace();

}

}

}publicstaticvoidmain(String[] args) {

TestFile testFile=newTestFile();//testFile.move(new File("F:\\dest1\\CentOS-5.5-i386-bin-DVD.iso"), new File("E:\\CentOS-5.5-i386-bin-DVD.iso"));testFile.runUnFinished(newFile("F:\\dest1\\CentOS-5.5-i386-bin-DVD.iso"),newFile("E:\\CentOS-5.5-i386-bin-DVD.iso"));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值