Chapter 10
Chapter Contents
Chapter Objectives
10.1 Introductory Example: Weather Data Analysis
10.2 Java/s I/O System: Readers, Writers, Streams
10.3 Exceptions
10.4 More About I/O Streams
10.5 Example: Scanning for a Virus
10.6 Example: Student Data Retrieval
10.7 Graphical/Internet Java: Student Data Retrieval GUI
Part of the Picture: Data Base Systems
Chapter Objectives
Study
Requirements
memory
Weatherman
Solution Overview
Note
Solution Overview
Opening text output file
PrintWriter out = new PrintWriter (
new BufferedWriter(
new Filewriter (args[1])));
Writing text values to a file
out.println ( . . . );
Closing the
in.close();
files
out.close();
Handling Exceptions
Many
files
When
Handling Exceptions
A try
This
Input
Stream
Executing Program
Predefined Streams
System.in
System.out
System.err
Wrapping Classes
class provides methods for
reading bytes only
InputStream
Example:
BufferedReader class
BufferedReader myReader =
new (BufferedReader (
new InputStreamReader( System.in));
Now
Using a Reader
Wrap
FileReader in BufferedReader
class
BufferedReader inFile =
new BufferedReader (
new FilReader (input_filename) );
Now
Using a Reader
How
Also
Using a Reader
Sequence of steps for using text input
1.
2.
3.
Using a Writer
When
10.3 Exceptions
Easy
faulty logic
bad input from user, etc.
Java
Try-Catch Blocks
Try-Catch Blocks
After catch
If
If
block executed
no exception thrown
catch blocks skipped
execution continues in finally { } or in
statements following catch block
Exception Hierarchy
Most general class
E x c e p t io n
D a t a F o r m a t E x c e p t io n
E O F E x c e p t io n
I O E x c e p t io n
F ile N o t F o u n d E x c e p t io n
. . .
. . .
Exceptions become
more specialized
R u n t im e E x c e p t io n
I n t e r r u p t e d I O E x c e p t io n
Try/Catch Strategy
Wrap
Wrap
Throwing Exceptions
Method
Java
Consider
numbers.dat
wrapping of Stream I/O classes
numbers.dat
appearto
tobe
begibberish?
gibberish?
use of try-catch blocks appear
specific methods used to write, then read
sample run
Possible
to create a class
is a virus?
A virus
Virus-Detection Algorithm
1.
2.
Observe
use of try/catch
wrapping of classes for file read
10.6 Example:
Retrieving Student Information
Problem:
Information Retrieval
Records of information about students consist of
We
Objects
Objects
Type
Kind
Name
varying
Number of
students
int
varying
numberOfStudents
Sequence of
students
StudentSequence
varying
students
constant
args[0]
A student ID
number
int
varying
studentID
Position of the
student
int
varying
position
Student Class
Operations
needed include
Note
Program Algorithm
1.
2.
Class StudentSequence
Note
Provides
Forever
loop used to
Enter Student ID :
Bill
Freshman
111223333
Board
Credits
16.0
GPA 3.15
Implementation
Methods
for GUIStudentRetreiver
constructor
action handler
enter initial state
enter found an ID state
enter not found state
Attributes
labels
entry fields
panes for panels
Event-Handler Method
public void actionPerformed(ActionEvent event)
{ String idString = myIdField.getText();
if (idString.equals(""))
enterInitialState();
else
{ int id = Integer.parseInt(idString);
int position = myStudents.find(id);
if (position >= 0)
enterGoodIDState(myStudents.getStudent(position));
else
enterErrorState("No student found with that ID");
}
}
Note
Notesource
sourcecode,
code,Figure
Figure10.8
10.8
on data
Data
DBMS Facilities
High
query language
Support
Security
Data
sharing
Data
integrity
Relational Model
Database
relations
SQL,
Example
All records
Name of
Relation
Condition