81,095
社区成员
发帖
与我相关
我的任务
分享
private static boolean copy(File soureFile,File distFile) throws Exception{
FileInputStream in = new FileInputStream(soureFile);
FileOutputStream out = new FileOutputStream(distFile);
FileChannel inC = in.getChannel();
FileChannel outC = out.getChannel();
ByteBuffer b = null;
int length = 2097152;
while (true) {
if (inC.position() == inC.size()) {
inC.close();
outC.close();
return true;
}
if ((inC.size() - inC.position()) < length) {
length = (int) (inC.size() - inC.position());
} else {
length = 2097152;
b = ByteBuffer.allocateDirect(length);
inC.read(b);
b.flip();
outC.write(b);
outC.force(false);
}
}
}
private static boolean move(File soureFile,File distFile) throws Exception{
FileInputStream in = new FileInputStream(soureFile);
FileOutputStream out = new FileOutputStream(distFile);
FileChannel inC = in.getChannel();
FileChannel outC = out.getChannel();
ByteBuffer b = null;
int length = 2097152;
while (true) {
if (inC.position() == inC.size()) {
inC.close();
outC.close();
soureFile.delete();
return true;
}
if ((inC.size() - inC.position()) < length) {
length = (int) (inC.size() - inC.position());
} else {
length = 2097152;
b = ByteBuffer.allocateDirect(length);
inC.read(b);
b.flip();
outC.write(b);
outC.force(false);
}
}
}