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.2. File Channel Read Operations

You obtain a reference to a FileChannel object that you can use to read a file by calling the getChannel() method of a FileInputStream object. Because a FileInputStream object opens a file as read-only, only channel read operations are legal. The channel returned by a FileInputStream object has three basic read operations available, each of which reads bytes starting at the byte indicated by the current position in the file. The file position will be incremented by the number of bytes read. The three read() methods for a FileChannel object are:

read(ByteBuffer buf)Tries to read buf.remaining() bytes (equivalent to limit-position bytes) from the file into the buffer, buf, starting at the buffer's current position. The number of bytes read is returned as type int. The value is −1 if the channel reaches the end-of-file during the operation. The buffer position will be incremented by the number of bytes read and the buffer's limit will be left unchanged.
read(ByteBuffer[] buffers)Tries to read bytes into each of the buffers in the buffers array in sequence. Bytes will be read into each buffer starting at the point defined by that buffer's position. The number of bytes read into each buffer is defined by the remaining() method for that buffer. The read() method returns the total number of bytes read as type int, or −1 if the channel reaches the end-of-file during the operation. Each buffer's position will be incremented by the number of bytes read ...

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