NIOラーニング:buffer読み込みと書き出し(ファイルコピー例)

1804 ワード

     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();