Java Reference
In-Depth Information
TOPIC
CONCEPT
Reading
Data from
a File
The data that you read from a file is stored in a buffer of type
ByteBuffer
.
Interpreting
Data from
a File
You can call one of the
get
methods for a
ByteBuffer
object to retrieve bytes from the buffer interpreted
as a given basic type.
Using
View
Buffers
A view buffer interprets the contents of a
ByteBuffer
as a sequence of data items of a given type. You can
use view buffers to interpret the bytes read from a file into a
ByteBuffer
as any basic type other than
boolean
. You obtain a view buffer corresponding to a
ByteBuffer
object for a given data type by calling
the appropriate
ByteBuffer
class method.
Memory-
Mapped
Files
A memory-mapped file enables you to access data in the file as though it were resident in memory. You ac-
cess a memory-mapped file through a
MappedByteBuffer
object that you obtain by calling the
map()
method for a
FileChannel
object.
File Lock-
ing
You can lock all or part of the contents of a file by acquiring a file lock. A file lock can be an exclusive
lock that locks the entire file for your use, or it can be a shared lock that locks part of a file while you are
accessing it.
Exclusive
File Locks
You can obtain an exclusive lock only on a file you have opened for both reading and writing. You obtain
an exclusive lock by calling the
lock()
method for the
FileChannel
object that is connected to the file.
Shared File
Locks
You obtain a shared file lock by calling the
tryLock()
method for the
FileChannel
object that is connec-
ted to the file.