O'Reilly logo

Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition by Ivor Horton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

11.6. Copying Files

You probably don't need a file copy program, as your operating system is bound to provide a facility for this. However, it is a useful way of demonstrating how a file channel for any input file can transfer data directly to a file channel for an output file without involving explicit buffers.

A file channel defines two methods for direct data transfer:

transferTo(long position,
           long count,
           WritableByteChannel dst)
Attempts to transfer count bytes from this channel to the channel dst. Bytes are read from this channel starting at the file position specified by position. The position of this channel is not altered by this operation, but the position of dst will be incremented by the number of bytes written. Fewer than count bytes will be transferred if this channel's file has fewer than count bytes remaining, or if dst is non-blocking and has fewer than count bytes free in its system output buffer. The number of bytes transferred is returned as a value of type int.
transferFrom(ReadableByteChannel src,
             long position,
             long count)
Attempts to transfer count bytes to this channel from the channel src. Bytes are written to this channel starting at the file position specified by position. The position of this channel is not altered by the operation, but the position of src will be incremented by the number of bytes read from it. If position is greater than the size of the file, then no bytes will be transferred. Fewer than count bytes will be transferred if the file ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required