美文网首页
java nio复制文件

java nio复制文件

作者: 不知不怪 | 来源:发表于2019-10-14 17:16 被阅读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);
        }
    }

相关文章

网友评论

      本文标题:java nio复制文件

      本文链接:https://www.haomeiwen.com/subject/flarmctx.html