Java Reference
In-Depth Information
F
IGURE
12.13
The program displays the number of characters, words, and lines in the given
file.
*12.14
(
Process scores in a text file
) Suppose that a text file contains an unspecified
number of scores separated by blanks. Write a program that prompts the user
to enter the file, reads the scores from the file, and displays their total and
average.
*12.15
(
Write/read data
) Write a program to create a file named
Exercise12_15.txt
if
it does not exist. Write
100
integers created randomly into the file using text
I/O. Integers are separated by spaces in the file. Read the data back from the
file and display the data in increasing order.
**12.16
(
Replace text
) Listing 12.16, ReplaceText.java, gives a program that replaces
text in a source file and saves the change into a new file. Revise the program to
save the change into the original file. For example, invoking
java Exercise12_16 file oldString newString
replaces
oldString
in the source file with
newString
.
***12.17
(
Game: hangman
) Rewrite Programming Exercise 7.35. The program reads the
words stored in a text file named
hangman.txt
. Words are delimited by spaces.
**12.18
(
Add package statement
) Suppose you have Java source files under the direc-
tories
chapter1
,
chapter2
, . . . ,
chapter34
. Write a program to insert the
statement
package chapteri;
as the first line for each Java source file under
the directory
chapteri
. Suppose
chapter1
,
chapter2
, . . . ,
chapter34
are under the root directory
srcRootDirectory
. The root directory and
chapteri
directory may contain other folders and files. Use the following
command to run the program:
java Exercise12_18 srcRootDirectory
*12.19
(
Count words
) Write a program that counts the number of words in President
Abraham Lincoln's Gettysburg address from
http://cs.armstrong.edu/liang/data/
**12.20
(
Remove package statement
) Suppose you have Java source files under the
directories
chapter1
,
chapter2
, . . . ,
chapter34
. Write a program to
remove the statement
package chapteri;
in the first line for each Java
source file under the directory
chapteri
. Suppose
chapter1
,
chapter2
,
. . . ,
chapter34
are under the root directory
srcRootDirectory
. The root
directory and
chapteri
directory may contain other folders and files. Use
the following command to run the program:
java Exercise12_20 srcRootDirectory
*12.21
(
Data sorted?
) Write a program that reads the strings from file
SortedStrings.
txt
and reports whether the strings in the files are stored in increasing order.
Search WWH ::
Custom Search