Self-Review Exercises
15.1 Determine whether each of the following statements is true or false. If false, explain why.
a) You must explicitlycreate the stream objects System.in, System.out and System.err. (False,These three streams are created for you when a Java application beginsexecuting.)
b) When reading data froma file using class Scanner, if you wish to read data in the file multiple times, the file must be closed and reopenedto read from the beginning of the file. (True)
c) Files static method exists receives a Path and determines whether it exists (either as a file or as adirectory) on disk (True)
d) Binary files are human readable in a text editor. (False, Text files are human readable in atext editor. Binary files might be human readable, but only if the bytes in thefile represent ASCII characters.)
e) An absolute pathcontains all the directories, starting with the root directory, that lead to aspecific file or directory. (True)
f) Class Formatter contains method printf, which enables formatted data to be output to the screen or toa file. (False, Class Formatter contains method format, which enables formatteddata to be output to the screen or to a file.)
15.2 Complete the following tasks, assuming that each applies to thesame program:
a) Write a statement thatopens file "oldmast.txt" for input—use Scanner variable inOldMaster.
Scanner inOldMaster = new Scanner(Paths.get("oldmast.txt"));
b) Write a statement thatopens file "trans.txt" for input—use Scanner variable inTransaction.
Scanner inOldMaster = new Scanner(Paths.get("oldmast.txt"));
c) Write a statement thatopens file "newmast.txt" for output (and creation)—use formatter variable outNewMaster.
Formatter outNewMaster= new Formatter("newmast.txt");
d) Write the statementsneeded to read a record from the file "oldmast.txt". Use the data to createan object of class Account—use Scanner variable inOldMaster. Assume that class Account is the same as the Account class in Fig. 15.9.
Account account = new Account();
account.setAccount(inOldMaster.nextInt());
account.setFirstName(inOldMaster.next());
account.setLastName(inOldMaster.next());
account.setBalance(inOldMaster.nextDouble());
e) Write the statementsneeded to read a record from the file "trans.txt". The record is an objectof class TransactionRecord—use Scanner variable inTransaction. Assume that class TransactionRecord contains method setAccount (which takes an int) to set the accountnumber and method setAmount (which takes a double) to set the amount ofthe transaction.
Account—use Formatter variable outNewMaster.
TransactionRecordtransaction = new Transaction();
transaction.setAccount(inTransaction.nextInt());
transaction.setAmount(inTransaction.nextDouble());
f) Write a statement thatoutputs a record to the file "newmast.txt". The record is an object of type
outNewMaster.format("%d %s %s %.2f%n",
account.getAccount(),account.getFirstName(),
account.getLastName(),account.getBalance());
15.3 Complete the following tasks, assuming that each applies to thesame program:
a) Write a statement that opens file "oldmast.ser" for input—use ObjectInputStream variable inOldMaster to wrap an InputStream object.
b) Write a statement that opens file "trans.ser" for input—use ObjectInputStream variable inTransaction to wrap an InputStream object.
c) Write a statement thatopens file "newmast.ser" for output (and creation)—use ObjectOutputStream variable outNewMaster to wrap an OutputStream.
d) Write a statement thatreads a record from the file "oldmast.ser". The record is an objectof class Account—use ObjectInputStream variable inOldMaster. Assume class Account is the same as the Account class in Fig. 15.9
e) Write a statement thatreads 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 oftype Account to the file "newmast.ser"—use ObjectOutputStream variable outNewMaster.
a) ObjectInputStreaminOldMaster = new ObjectInputStream(
Files.newInputStream(Paths.get("oldmast.ser")));
b) ObjectInputStreaminTransaction = new ObjectInputStream(
Files.newOutputStream(Paths.get("trans.ser")));
c) ObjectOutputStreamoutNewMaster = new ObjectOutputStream(
Files.newOutputStream(Paths.get("newmast.ser")));
d) Account = (Account)inOldMaster.readObject();
e) transactionRecord =(TransactionRecord) inTransaction.readObject();
f) outNewMaster.writeObject(newAccount);