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"));
}
}