public class ThreadFileCopy extends Thread{
private File source;
private File target;
long start;
long end;
public ThreadFileCopy(File source, File target, long start, long end) {
super();
this.source = source;
this.target = target;
this.start = start;
this.end = end;
}
@Override
public void run() {
System.out.println(this.getName()+"开始拷贝");
int count = 0;
RandomAccessFile input = null;
RandomAccessFile output = null;
try {
input = new RandomAccessFile(source, "r");
output = new RandomAccessFile(target, "rw");
input.seek(start);
output.seek(start);
byte[] b = new byte[1024];
int len = 0;
while(((len = input.read(b)) != -1 )&& (count <= end - start))
{
count += len;
output.write(b, 0, len);
}
System.out.println(this.getName()+"拷贝结束"+count);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
}finally {
try {
if(output!=null)output.close();
if(input!=null)input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
File source = new File("C:\\Users\\lx\\Desktop\\larva搞笑虫子.rar");
File target = new File("C:\\Users\\lx\\Desktop\\新建文件夹",source.getName());
long item = source.length()/4;
for(int i = 0;i<4;i++)
{
new ThreadFileCopy(source, target, i*item, (i+1)*item).start();
}
}
}