Write a method that could be added to the TextIO program to read a text file and print all lines containing a certain word. This should be a void method that takes two parameters: The name of the file and the word to search for. Lines not containing the word should not be printed.
15.File Statistics.
Write a program that reads a text file and reports the number of characters and lines contained in the file.
16.Word Counter.
Modify the program in the previous exercise so that it also counts the number of words in the file. Hint.
StringTokenizer
17.Student Saver.
Modify the ObjectIO program so that it allows the user to designate a file and then input Student data with the help of a GUI. As the user inputs data, each record should be written to the file.
18.Sort ints File.
Write a program that will read a file of ints into memory, sort them in ascending order, and output the sorted data to a second file.
19.Sorted Merge Files.
Write a program that will read two files of ints, which are already sorted into ascending order, and merge their data. For example, if one file contains 1, 3, 5, 7, 9, and the other contains 2, 4, 6, 8, 10, then the merged file should contain 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
20.Read File Method.
Suppose you have a file of data for a geological survey. Each record consists of a longitude, a latitude, and an amount of rainfall, all represented by doubles. Write a method to read this file’s data and print them on the screen, one record per line. The method should be void and it should take the name of the file as its only parameter.
21.Generate Records to File.
Suppose you have the same data as in the previous exercise. Write a method that will generate 1,000 records of random data and write them to a file. The method should be void and should take the file’s name as its parameter. Assume that longitudes have values in the range \(+/-\) 0 to 180 degrees, latitudes have values in the range \(+/-\) 0 to 90 degrees, and rainfalls have values in the range 0 to 20 inches.
22.Safe file Copy.
Design and write a file copy program that will work for either text files or binary files. The program should prompt the user for the names of each file and copy the data from the source file into the destination file. It should not overwrite an existing file, however. Hint.
byte
23.Address Class.
Design a class, similar to Student, to represent an Address. It should consist of street, city, state, and zip code and should contain its own readFromFile() and writeToFile() methods.
24.Student with Address.
Using the class designed in the previous exercise, modify the Student class so that it contains an Address field. Modify the ObjectIO program to accommodate this new definition of Student and test your program.
25.Directory Listing.
Write a program called Directory, which provides a listing of any directory contained in the current directory. This program should prompt the user for the name of the directory. It should then print a listing of that directory. The listing should contain the following information: The full path name of the directory, and then include the file name, length, and last modified date, and a read/write code for each file. The read/write code should be an r if the file is readable and a w if the file is writeable, in that order. Use a “-” to indicate not readable or not writeable. For example, a file that is readable but not writable will have the code r-. Here’s an example listing:
Note that the File.lastModified() returns a long, which gives the modification time of the file. This number can’t easily be converted into a date, so just report its value.
26.Save OneRowNim Game.
Challenge: Modify the OneRowNimGUI class that is listed in Chapter 4’s Figure 4-25 so that the user can save the position of the game to a file or open and read a game position from a file. You should add two new JButtons to the GUI interface. Use the object serialization example as a model for your input and output streams.
27.Java Grep Program.
Challenge: In Unix systems, there’s a program named grep that can list the lines in a text file containing a certain string. Write a Java version of this program that prompts the user for the name of the file and the string to search for.
28.Copy File to file or directory.
Challenge: Write a program in Java named Copy to copy one file into another. The program should prompt the user for two file names, filename1 and filename2. Both filename1 and filename2 must exist or the program should throw a FileNotFoundException. Although filename1 must be the name of a file (not a directory), filename2 may be either a file or a directory. If filename2 is a file, then it must not already exist, and then the program should copy filename1 to filename2. If filename2 is a directory, then the program should make sure that filename1 is not in filename2 then simply copy filename1 into filename2. That is, it should create a new file with the name filename1 inside the filename2 directory, and copy the old file to the new file.