Exercises

  • 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 line.

  • 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 words).

  • 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 directories.

  • Exercise 3-7. Write a program named Uncompress that uncompresses files and directories compressed by the Compress example in this ...

Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.