package com.yangtao.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
public class TestFile {
private static int SIZE = 1024 * 1024 * 2;
public void move(File srcFile, File destFile) {
try {
int length = SIZE;
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(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()) < SIZE * 10) {
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();
}
}
public void runUnFinished(File srcFile, File destFile) {
if (destFile.exists()) {
try {
RandomAccessFile randomSrcFile = new RandomAccessFile(srcFile,"rw");
long destFileSize = destFile.length();
RandomAccessFile randomDestFile = new RandomAccessFile(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) {
int length = SIZE;
if (fin.position() == fin.size()) {
fin.close();
fout.close();
break;
}
if ((fin.size() - fin.position()) < SIZE * 10) {
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 block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
TestFile testFile = new TestFile();
// 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(new File(
"F:\\dest1\\CentOS-5.5-i386-bin-DVD.iso"), new File(
"E:\\CentOS-5.5-i386-bin-DVD.iso"));
}
}