private void copyFile(String source, String destination)
throws IOException {
FileInputStream fis =
new FileInputStream(source);
FileChannel fci =
fis.getChannel();
FileOutputStream fos =
new FileOutputStream(destination);
FileChannel fco =
fos.getChannel();
// create a buffer with 2048 size for buffering data
ByteBuffer buffer = ByteBuffer.allocate(2048
);
int b =
fci.read(buffer);
while(b != -1) {
// reach the end of the channel?
// flip the buffer
buffer.flip();
// write to the destination channel
fco.write(buffer);
// clear the buffer and use it for the next read process
buffer.clear();
// read a block of data and put it in the buffer
b =
fci.read(buffer);
}
fis.close();
fos.close();
}
转载于:https://www.cnblogs.com/ouxingning/archive/2012/09/05/use_nio_to_copy_file.html