Chapter 8: Exceptions and I/O Streams Java Software Solutions Second Edition
Download
Report
Transcript Chapter 8: Exceptions and I/O Streams Java Software Solutions Second Edition
Chapter 8: Exceptions and I/O Streams
Presentation slides for
Java Software Solutions
Foundations of Program Design
Second Edition
by John Lewis and William Loftus
Java Software Solutions is published by Addison-Wesley
Presentation slides are copyright 2000 by John Lewis and William Loftus. All rights reserved.
Instructors using the textbook may use and modify these slides for pedagogical purposes.
I/O Streams
A stream is a sequence of bytes that flow from a source to a
destination
In a program, we read information from an input stream
and write information to an output stream
A program can manage multiple streams at a time
The java.io package contains many classes that allow us to
define various streams with specific characteristics
I/O Stream Categories
The classes in the I/O package divide input and output
streams into other categories
An I/O stream is either a
• character stream, which deals with text data
• byte stream, which deal with byte data
An I/O stream is also either a
• data stream, which acts as either a source or destination
• processing stream, which alters or manages information in the
stream
Standard I/O
There are three standard I/O streams:
• standard input – defined by System.in
• standard output – defined by System.out
• standard error – defined by System.err
We use System.out when we execute println
statements
System.in is declared to be a generic InputStream
reference, and therefore usually must be mapped to a more
useful stream with specific characteristics
The Keyboard Class
The Keyboard class was written by the authors of your
textbook to facilitate reading data from standard input
Now we can examine the processing of the Keyboard class
in more detail
The Keyboard class:
•
•
•
•
•
declares a useful standard input stream
handles exceptions that may be thrown
parses input lines into separate values
converts input stings into the expected type
handles conversion problems
The Standard Input Stream
The Keyboard class declares the following input stream:
InputStreamReader isr =
new InputStreamReader (System.in)
BufferedReader stdin = new BufferedReader (isr);
The InputStreamReader object converts the original
byte stream into a character stream
The BufferedReader object allows us to use the
readLine method to get an entire line of input
Text Files
Information can be read from and written to text files by
declaring and using the correct I/O streams
The FileReader class represents an input file containing
character data
See Inventory.java (page 397)
See InventoryItem.java (page 400)
The FileWriter class represents a text output file
See TestData.java (page 402)
Object Serialization
Object serialization is the act of saving an object, and its
current state, so that it can be used again in another
program
The idea that an object can “live” beyond the program that
created it is called persistence
Object serialization is accomplished using the classes
ObjectOutputStream and ObjectInputStream
Serialization takes into account any other objects that are
referenced by an object being serialized, saving them too