In this chapter, I have discussed the various ways in which you can read basic types of data from a file. The important points I have discussed include:
You can read a file using a FileChannel object obtained from a FileInputStream object or from a RandomAccessFile object.
You can use a channel obtained from a RandomAccessFile object that was created with mode "rw", "rwd", or "rws" to both read and write the file.
Data that is read from a file using a channel is stored in one or more buffers of type ByteBuffer.
You can use view buffers to interpret the data read from a file as any basic type other than boolean.
A memory-mapped file enables you to access data in the file as though it were resident in memory. You access a memory-mapped file through a MappedByteBuffer object.
Acquiring an exclusive lock on a file ensures that no other program can access the file while you hold the lock. You can obtain an exclusive lock only on a file you have opened for both reading and writing.
Acquiring a shared lock on a file ensures that your program has access to the file in circumstances where other programs may be accessing the same file, and protects the region of the file you have locked from being altered.