JHTP自测题_第十五章_文件、流和对象序列化(Files, Streams, and Object Serialization)


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 inputuse Scanner variable inOldMaster. 

Scanner inOldMaster = new Scanner(Paths.get("oldmast.txt"));

b) Write a statement thatopens file "trans.txt" for inputuse 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 Accountuse 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 TransactionRecorduse 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.

Accountuse 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 inputuse ObjectInputStream  variable inOldMaster to wrap an InputStream object.

 b) Write a statement that opens file "trans.ser" for inputuse 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 Accountuse 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 TransactionRecorduse 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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值