|
14.1
|
Fill in the blanks in each of the following statements:
-
Ultimately, all data items
processed
by a computer are reduced to combinations of __________ and __________.
-
The smallest data item a computer can process is called a(n)__________.
-
A(n) __________ can sometimes be
viewed
as a
group
of
related
records.
-
Digits,
letters
and special symbols are referred to as __________.
-
A group of related files is called a __________.
-
Object __________ normally enables a program to output error messages to the screen.
-
RandomAccessFile
method __________ reads an integer from the specified stream.
-
RandomAccessFile
method __________ sets the file-position pointer to a specific location in a file for input or output.
|
|
14.2
|
Determine which of the following statements are
true
and which are
false
. If
false
, explain why.
-
The programmer must explicitly create the stream objects
System.in
,
System.out
and
System.err
.
-
When reading data from a file using class
Scanner
, if the programmer wishes to read data in the file multiple times, the file must be closed and reopened to read from the beginning of the file. This moves the file-position pointer back to the beginning of the file.
-
Method
exists
of class
File
returns
TRue
if the
name
specified as the argument to the
File
constructor is a file or directory in the specified
path
.
-
It is not necessary to search all the records in a random-access file to find a record.
-
Binary files are human readable.
-
An absolute path contains all the directories, starting with the root directory, that lead to a specific file or directory.
-
Class
Formatter
contains method
printf
, which enables formatted data to be output to the screen or to a file.
|
|
14.3
|
Complete the following
tasks
,
assuming
that each applies to the same program:
-
Write a statement that opens file
"oldmast.txt"
for inputuse
Scanner
variable
inOldMaster
.
-
Write a statement that opens file
"trans.txt"
for inputuse
Scanner
variable
inTransaction
.
-
Write a statement that opens file
"newmast.txt"
for output (and creation)use
formatter
variable
outNewMaster
.
-
Write the statements needed to read a record from the file
"oldmast.txt"
. The data read should be used to create an object of class
AccountRecord
use
Scanner
variable
inOldMaster
. Assume that class
AccountRecord
is the same as the
AccountRecord
class in Fig. 14.6.
-
Write the statements needed to read a record from the file
"trans.txt"
. The record is an object of class
transactionRecord
use
Scanner
variable
inTransaction
. Assume that class
transactionRecord
contains method
setAccount
(which takes an
int
) to set the account number and method
setAmount
(which takes a
double
) to set the amount of the transaction.
-
Write a statement that outputs a record to the file
"newmast.txt"
. The record is an object of type
AccountRecord
use
Formatter
variable
outNewMaster
.
|
|
14.4
|
Complete the following tasks, assuming that each applies to the same program:
-
Write a statement that opens file
"oldmast.ser"
for inputuse
ObjectInputStream
variable
inOldMaster
to wrap a
FileInputStream
object.
[Page 738]
-
Write a statement that opens file
"trans.ser"
for inputuse
ObjectInputStream
variable
inTransaction
to wrap a
FileInputStream
object.
-
Write a statement that opens file
"newmast.ser"
for output (and creation)use
ObjectOutputStream
variable
outNewMaster
to wrap a
FileOutputStream
.
-
Write a statement that reads a record from the file
"oldmast.ser"
. The record is an object of class
AccountRecordSerializable
use
ObjectInputStream
variable
inOldMaster
. Assume class
AccountRecordSerializable
is the same as the
AccountRecordSerializable
class in Fig. 14.17
-
Write a statement that reads a record from the file
"trans.ser"
. The record is an object of class
transactionRecord
use
ObjectInputStream
variable
inTransaction
.
-
Write a statement that outputs a record to the file
"newmast.ser"
. The record is an object of type
AccountRecordSerializable
use
ObjectOutputStream
variable
outNewMaster
.
|
|
14.5
|
Find the error in each block of code and show how to correct it.
-
Assume that
account
,
company
and
amount
are declared.
ObjectOutputStream outputStream;
outputStream.writeInt( account );
outputStream.writeChars( company );
outputStream.writeDouble( amount );
-
The following statements should read a record from the file
"payables.txt"
. The
Scanner
variable
inPayable
should be used to refer to this file.
Scanner inPayable =
new
Scanner(
new
File(
"payables.txt"
) );
PayablesRecord record = ( PayablesRecord ) inPayable.readObject();
|