Java Reference
In-Depth Information
Data that are 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.
Acquiring an exclusive lock on a file ensures that no other program can access the file while
you hold the lock.
Acquiring a shared lock on a file ensures your program has access to the file in circumstances
where other programs may be accessing the same file.
If you can't ride two horses at once, you shouldn't be in the circus.
Exercises
1.
Write a program to read back and list the contents of the file written by the first exercise in
the previous chapter.
2. Extend the ReadPrimes example that we produced in this chapter to optionally display the
nth prime, when n is entered from the keyboard.
3.
Extend the ReadPrimes program further to output a given number of primes, starting at a
given number. For example, output 15 primes starting at the 30th. The existing capabilities
should be retained.
4.
Write a program that will output the contents of a file to the command line as groups of eight
hexadecimal digits with five groups to a line, each group separated from the next by a space.
5. Write a program that will allow either one or more names and addresses to be entered from
the keyboard and appended to a file, or the contents of the file to be read back and output to
the command line.
6.
Modify the previous example to store an index to the name and address file in a separate file.
The index file should contain each person's second name, plus the position where the
corresponding name and address can be found in the name and address file. Provide support
for an optional command argument allowing a person's second name to be entered. When the
command line argument is present, the program should then find the name and address and
output it to the command line.
Search WWH ::




Custom Search