import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class Test {
public static void main(String[] args) throws IOException {
RandomAccessFile inra = new RandomAccessFile("E:\\Linux\\3月13日\\镜像文件\\CentOS-7-x86_64-DVD-1611.iso", "r");
RandomAccessFile outra = new RandomAccessFile("D:\\Linux\\镜像文件\\CentOS-7-x86_64-DVD-1611.iso", "rw");
FileChannel inraChannel = inra.getChannel();
FileChannel outraChannel = outra.getChannel();
long size = inraChannel.size();
System.out.println(size);
long count = 1;
long startindex = 0;
long everysize = 512 * 1024 * 1024;
long copysize = size;
if (size > everysize) {
count = size % everysize == 0 ? size / everysize : size / everysize + 1;
copysize = everysize;
}
for (int i = 0; i < count; i++) {
MappedByteBuffer inmap = inraChannel.map(FileChannel.MapMode.READ_ONLY, startindex, copysize);
MappedByteBuffer outmap = outraChannel.map(FileChannel.MapMode.READ_WRITE, startindex, copysize);
System.out.println("每块文件大小:" + copysize);
System.out.println("每块文件开始复制的索引:" + startindex);
for (int j = 0; j < copysize; j++) {
byte b = inmap.get(j);
outmap.put(j, b);
}
startindex += copysize;
copysize = size - startindex > everysize ? everysize : size - startindex;
}
inra.close();
outra.close();
inraChannel.close();
outraChannel.close();
}
}