Exercise 3-1. Write a program named Head that prints out the
first 10 lines of each file specified on the command line.
Exercise 3-2. Write a corresponding program named Tail that
prints out the last 10 lines of each file specified on the command
Exercise 3-3. Write a program that counts and reports the number of lines, words,
and characters in a specified file. Use static methods of the
java.lang.Character class to determine whether a
given character is a space (and therefore the boundary between two
Exercise 3-4. Write a program that adds up and reports the size of all files in a
specified directory. It should recursively scan any subdirectories,
summing and reporting the size of the files that they contain, and
incorporate those directory sizes into its final output.
Exercise 3-5. Write a program that lists all of the files and subdirectories in a
specified directory, along with their sizes and modification dates.
By default, the output should be sorted by name. If invoked with the
-s option, however, output should be sorted by
size, from largest to smallest. If invoked with the
-d option, output should be sorted by date, from
most recent to least. Use the sort( ) method of
java.util.Collections to help with the sorting.
Exercise 3-6. Modify the Compress program of Example 3-5 to make it recursively zip the contents of
Exercise 3-7. Write a program named Uncompress that uncompresses
files and directories compressed by the Compress
example in this chapter.
Exercise 3-8. Write a subclass of OutputStream named
TeeOutputStream that acts like a T joint in a
pipe; the stream sends its output to two different output streams,
specified when the TeeOutputStream is created.
Write a simple test program that uses two
TeeOutputStream objects to send text read from
System.in to System.out and to
two different test files.
Exercise 3-9. The WordList class of Example 3-8
uses a long to store the position of each word in
the file. It uses a long because this is the data
type used by the getFilePosition( ) and
seek( ) methods of
RandomAccessFile. It is inefficient to store the
entire 8 bytes of the long to the disk file,
however, since the strings are written sequentially and the positions
in the position[ ] array are strictly increasing.
Modify the example to store the differences between adjacent
positions instead of the positions themselves. This should cut the
index size in half (or more, if you choose to limit the length of
Assume that most (but not all) strings stored in a
WordList will be short and require less than 256
bytes of storage, meaning that the offsets between adjacent strings
will typically fit into one byte. Design and implement a compression
scheme that will further compress the size of the positions[
] array on disk by using a variable number of bytes to
store the position offset values. Note that because the length of the
position table is no longer fixed, it can no longer be stored at the
start of the WordList. Instead,
you'll need to save the positions at the end of the
file, and simply store the location of the positions at the start of