欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Util

程序员文章站 2022-05-26 14:06:00
...

NioCopy

    public  static  void nioCopyFile(String resource,String destination)
    throws  Exception{
     FileInputStream fis = new FileInputStream(resource);
        FileOutputStream fos = new FileOutputStream(destination);

        //得到readChannel
        FileChannel readChannel = fis.getChannel();
        //得到writeChannel
        FileChannel writeChannel = fos.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while(true){
            buffer.clear();
            int len = readChannel.read(buffer);
            if(len==-1){
                break;
            }
            buffer.flip();//read--->write
            writeChannel.write(buffer);
        }
        readChannel.close();
        writeChannel.close();
    }

 

相关标签: 工具方法