Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

6. New I/O > 6.4. File Copying with Buffers

File Copying with Buffers

Example 6-4 is another file copying program. Instead of using the shortcut FileChannel.transferTo( ) method, this example defines a generic copy( ) method for copying bytes from one channel to another and demonstrates the basic java.nio I/O loop, described here:

  1. The buffer is filled with bytes from one channel.

  2. The buffer is flipped, making it ready to be drained. See Buffer and Buffer.flip( ) for details.

  3. The buffer is drained by writing bytes from it to another channel.

  4. The buffer is compacted, discarding bytes that have been drained from it and shifting remaining bytes to the beginning of the buffer. As part of this process, the current position of the buffer is reset to point to the first available byte in the buffer, making the buffer ready to be filled again. See ByteBuffer.compact( ) for details. If the call to write( ) completely drained the buffer, then clear( ) can be used instead of compact( ).


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint