NIO学习:buffer读入与写出(文件复制示例)

     FileInputStream fInputStream=new FileInputStream(new File("/root/Desktop/testImage.jpg"));
        FileOutputStream fOutputStream =new FileOutputStream(new File("/root/Desktop/testImage2.jpg"));
        FileChannel fcIn=fInputStream.getChannel();
        FileChannel fcOut=fOutputStream.getChannel();
        ByteBuffer buffer=ByteBuffer.allocate(1024);
        //将管道的数据读入buffer中
        while(fcIn.read(buffer)!=-1){
            //由读操作转到写操作时必须调用flip来改变buffer的状态
            //内部操作:limit=position;position=0
            buffer.flip();
            //把buffer的数据写入管道
            fcOut.write(buffer);
            //清空buffer
            //内部操作:limit=capacity;position=0
            buffer.clear();
        }
        fcIn.close();
        fInputStream.close();
        fcOut.close();
        fOutputStream.close();
原文地址:https://www.cnblogs.com/DajiangDev/p/3921601.html