Java Reference
In-Depth Information
Section 17.6
*17.5
(
Store objects and arrays in a file
) Write a program that stores an array of the five
int
values
1
,
2
,
3
,
4,
and
5
, a
Date
object for the current time, and the
double
value
5.5
into the file named
Exercise17_05.dat
.
*17.6
(
Store
Loan
objects
) The
Loan
class in Listing 10.2 does not implement
Serializable
. Rewrite the
Loan
class to implement
Serializable
. Write
a program that creates five
Loan
objects and stores them in a file named
Exercise17_06.dat
.
*17.7
(
Restore objects from a file
) Suppose a file named
Exercise17_07.dat
has been
created using the
ObjectOutputStream
. The file contains
Loan
objects. The
Loan
class in Listing 10.2 does not implement
Serializable
. Rewrite the
Loan
class to implement
Serializable
. Write a program that reads the
Loan
objects from the file and displays the total loan amount. Suppose you don't
know how many
Loan
objects are there in the file, use
EOFException
to end
the loop.
Section 17.7
*17.8
(
Update count
) Suppose you wish to track how many times a program has been
executed. You can store an
int
to count the file. Increase the count by
1
each
time this program is executed. Let the program be
Exercise17_08
and store the
count in
Exercise17_08.dat
.
***17.9
(
Address book
) Write a program that stores, retrieves, adds, and updates addresses
as shown in Figure 17.20. Use a fixed-length string for storing each attribute in the
address. Use random access file for reading and writing an address. Assume that
the size of name, street, city, state, and zip is 32, 32, 20, 2, 5 bytes, respectively.
F
IGURE
17.20
The application can store, retrieve, and update addresses from a file.
Comprehensive
*17.10
(
Split files
) Suppose you want to back up a huge file (e.g., a 10-GB AVI file) to a
CD-R. You can achieve it by splitting the file into smaller pieces and backing up
these pieces separately. Write a utility program that splits a large file into smaller
ones using the following command:
VideoNote
Split a large file
java Exercise17_10 SourceFile numberOfPieces
The command creates the files
SourceFile.1
,
SourceFile.2
, . . . ,
SourceFile.n
,
where
n
is
numberOfPieces
and the output files are about the same size.
**17.11
(
Split files GUI
) Rewrite Exercise 17.10 with a GUI, as shown in Figure 17.21a.
*17.12
(
Combine files
) Write a utility program that combines the files together into a
new file using the following command:
java Exercise17_12 SourceFile1 . . . SourceFilen TargetFile
The command combines SourceFile1, . . . , and SourceFilen into TargetFile.
Search WWH ::
Custom Search