Java Reference
In-Depth Information
c)
Write a statement that opens file "newmast.ser" for output (and creation)—use
ObjectOutputStream variable outNewMaster to wrap an OutputStream .
d)
Write a statement that reads a record from the file "oldmast.ser" . The record is an
object of class Account —use ObjectInputStream variable inOldMaster . Assume class
Account is the same as the Account class in Fig. 15.9
e)
Write a statement that reads a record from the file "trans.ser" . The record is an object
of class TransactionRecord —use ObjectInputStream variable inTransaction .
f)
Write a statement that outputs a record of type Account to the file "newmast.ser" —use
ObjectOutputStream variable outNewMaster .
Answers to Self-Review Exercises
15.1 a) False. These three streams are created for you when a Java application begins executing.
b) True. c) True. d) False. Text files are human readable in a text editor. Binary files might be hu-
man readable, but only if the bytes in the file represent ASCII characters. e) True. f) False. Class
Formatter contains method format , which enables formatted data to be output to the screen or to
a file.
15.2
a) Scanner inOldMaster = new Scanner(Paths.get( "oldmast.txt" ));
b) Scanner inTransaction = new Scanner(Paths.get( "trans.txt" ));
c) Formatter outNewMaster = new Formatter( "newmast.txt" );
d) Account account = new Account();
account.setAccount(inOldMaster.nextInt());
account.setFirstName(inOldMaster.next());
account.setLastName(inOldMaster.next());
account.setBalance(inOldMaster.nextDouble());
e) TransactionRecord transaction = new Transaction();
transaction.setAccount(inTransaction.nextInt());
transaction.setAmount(inTransaction.nextDouble());
f) outNewMaster.format( "%d %s %s %.2f%n" ,
account.getAccount(), account.getFirstName(),
account.getLastName(), account.getBalance());
15.3
a) ObjectInputStream inOldMaster = new ObjectInputStream(
Files.newInputStream(Paths.get( "oldmast.ser" )));
b) ObjectInputStream inTransaction = new ObjectInputStream(
Files.newOutputStream(Paths.get( "trans.ser") ));
c) ObjectOutputStream outNewMaster = new ObjectOutputStream(
Files.newOutputStream(Paths.get( "newmast.ser" )));
d) Account = (Account) inOldMaster.readObject();
e) transactionRecord = (TransactionRecord) inTransaction.readObject();
f) outNewMaster.writeObject(newAccount);
Exercises
15.4 (File Matching) Self-Review Exercise 15.2 asked you to write a series of single statements.
Actually, these statements form the core of an important type of file-processing program—namely,
a file-matching program. In commercial data processing, it's common to have several files in each
application system. In an accounts receivable system, for example, there's generally a master file con-
taining detailed information about each customer, such as the customer's name, address, telephone
number, outstanding balance, credit limit, discount terms, contract arrangements and possibly a
condensed history of recent purchases and cash payments.
 
Search WWH ::




Custom Search