Java Reference
In-Depth Information
•A SequenceInputStream concatenates several InputStream s. When the program reaches the end
of an input stream, that stream closes, and the next stream in the sequence opens.
•The Reader (p. 675) and Writer (p. 675) abstract classes are Unicode character-based streams.
Most byte-based streams have corresponding character-based concrete Reader or Writer classes.
• Classes BufferedReader (p. 675) and BufferedWriter (p. 675) buffer character-based streams.
• Classes CharArrayReader (p. 676) and CharArrayWriter (p. 676) manipulate char arrays.
•A LineNumberReader (p. 676) is a buffered character stream that tracks the number of lines read.
• Classes FileReader (p. 676) and FileWriter (p. 676) perform character-based file I/O.
• Class PipedReader (p. 676) and class PipedWriter (p. 676) implement piped-character streams
for transferring data between threads.
• Class StringReader (p. 676) and StringWriter (p. 676) read characters from and write charac-
ters to String s, respectively. A PrintWriter (p. 654) writes characters to a stream.
Self-Review Exercises
15.1
Determine whether each of the following statements is true or false . If false , explain why.
a) You must explicitly create the stream objects System.in , System.out and System.err .
b) When reading data from a file using class Scanner , if you wish to read data in the file mul-
tiple times, the file must be closed and reopened to read from the beginning of the file.
c) Files static method exists receives a Path and determines whether it exists (either as
a file or as a directory) on disk
d)
Binary files are human readable in a text editor.
e)
An absolute path contains all the directories, starting with the root directory, that lead
to a specific file or directory.
f)
Class Formatter contains method printf , which enables formatted data to be output
to the screen or to a file.
15.2
Complete the following tasks, assuming that each applies to the same program:
a)
Write a statement that opens file "oldmast.txt" for input—use Scanner variable in-
OldMaster .
b)
Write a statement that opens file "trans.txt" for input—use Scanner variable in-
Transaction .
c)
Write a statement that opens file "newmast.txt" for output (and creation)—use for-
matter variable outNewMaster .
d)
Write the statements needed to read a record from the file "oldmast.txt" . Use the data
to create an object of class Account —use Scanner variable inOldMaster . Assume that
class Account is the same as the Account class in Fig. 15.9.
e)
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.
f)
Write a statement that outputs a record to the file "newmast.txt" . The record is an ob-
ject of type Account —use Formatter variable outNewMaster .
15.3
Complete the following tasks, assuming that each applies to the same 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 vari-
able inTransaction to wrap an InputStream object.
Search WWH ::




Custom Search