java nio复制文件
2019-10-14 本文已影响0人
不知不怪
public void copyFile(String src, String des) {
try {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(des);
FileChannel inchannel = fis.getChannel();
FileChannel outchannel = fos.getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024 * 1024);
while (inchannel.read(buf) != -1) {
buf.flip();
outchannel.write(buf);
buf.clear();
}
outchannel.close();
inchannel.close();
fis.close();
fos.close();
} catch (FileNotFoundException e) {
log.info("复制文件时发FileNotFoundException导常!", e);
} catch (IOException e) {
log.info("复制文件时发IOException导常!", e);
}
}