Transcript Week11

COSC 1P02
Cosc 1P02
Binary Files & Persistent Objects
“For myself, I am an optimist--it does not seem to be
much use being anything else.”
Winston Churchill
Data Structures and Abstraction
2.1
COSC 1P02
Files
 collections of data stored on external media
 disk, tape, CD-ROM, DVD
 file management (O/S)
 external file name
 processing
 open
 internal file name
 file reference
 process
 close
 stream I/O vs record I/O
Data Structures and Abstraction
2.2
COSC 1P02
Record I/O
 field
 indivisible
 record
 database
 key
 natural key
 assigned key
 I/O of entire record from /to binary file
 record/structure variables
 object-oriented I/O
Data Structures and Abstraction
2.3
COSC 1P02
Record Output
 object I/O to binary streams only
 output
 constructor opens file
 external file name
 writeObject()
 subtype
 upcasting to Object
 always valid
 class must implement Serializable
 security
 interfaces
Data Structures and Abstraction
2.4
COSC 1P02
E.g. Create an Employee File
 Employee records
 Employee class from Example 7.2
 Serializable
 Class serial identification number
 SUID – explicitly stated.
 If not explicitly state:
 Created at compile time as a computed hash of various class
elements: class name, implemented interfaces, declared
nonstatic, nontransient fields, declared nonprivate methods,
and so on.
 data from a text file
 Read text file until EOF
 reads text record using Employee constructor
 writes Employee object using writeObject
Data Structures and Abstraction
2.5
COSC 1P02
Record Input
 readObject()
 must downcast to expected type
 not a conversion
 ClassCastException
 Matches serial number (SUID) of object to object’s class
specification
 Security
 Class Versioning.
Data Structures and Abstraction
2.6
COSC 1P02
E.g. List Employee File




not a text file so cannot view in text editor
number of pieces of work
report generation to EOF
no use of Employee constructor
 Employee’s are not new
 persistent objects
 serialization
 same class file
 can recompile Employee class
 If explicit serialVersionUID is used
 PayRoll with persistent objects
Data Structures and Abstraction
2.7
COSC 1P02
The End
Data Structures and Abstraction
2.14