Title: Binary%20Files%20
1Cosc 1P02
- Binary Files Persistent Objects
For myself, I am an optimist--it does not seem
to be much use being anything else. Winston
Churchill
2Files
- 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
3Record 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
4Record 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
5E.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
6Record Input
- readObject()
- must downcast to expected type
- not a conversion
- ClassCastException
- Matches serial number (SUID) of object to
objects class specification - Security
- Class Versioning.
7E.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
- Employees are not new
- persistent objects
- serialization
- same class file
- can recompile Employee class
- If explicit serialVersionUID is used
- PayRoll with persistent objects
8PayRoll Employee file.
1111 12.50 10000.00 2300.00 2222 5.50 4400.00 0.0
0 3333 7.50 3000.00 0.00 4444 45.00 36000.00 8280
.00
9(No Transcript)
10Employee Class
- package Example_7_2
- import java.io.
- import BasicIO.
- public class Employee implements Serializable
-
- private static final long serialVersionUID
123456L - private String empNum // employee number
- private double rate // pay rate
- private double ytdGross // year-to-date
gross pay - private double ytdTax // year-to-date
taxes paid -
- / The constructor creates a new employee
reading the employee - data from a file.
-
- _at_param from data file for employee data.
/ -
11EmployeeCreate
package Example_7_2 import
BasicIO. public class EmpCreate
private ASCIIDataFile in //
employee data file private BinaryOutputFile
out // new employee data file
public EmpCreate () Employee
aE in new ASCIIDataFile() out
new BinaryOutputFile() while
(true) aE new Employee(in) if
(in.isEOF()) break out.writeObject(aE)
in.close() out.close()
// construtor public
static void main ( String args ) new
EmpCreate()
12Employee List
- public class EmployeeList
-
- private BinaryDataFile empFile //
employee data file - private ASCIIDisplayer out
-
- public EmployeeList ( )
-
- empFile new BinaryDataFile()
- out new ASCIIDisplayer()
- // construtor
-
- public void run ( )
- Employee anEmployee // current employee
- while (true)
- anEmployee (Employee)empFile.readObject()
- if (empFile.isEOF()) break
- this.writeDetail(anEmployee)
-
- empFile.close()
13PayRoll
- private void runPayroll ( )
-
- int numEmp // number of
employees - Employee anEmployee // current
employee - double hours // hours worked
- double pay // weekly pay
- int button // button pressed
-
- numEmp 0
- while ( true )
- anEmployee (Employee)empFile.readObj
ect() - if ( empFile.isEOF()
!empFile.successful()) break - payForm.clearAll()
- fillForm(anEmployee)
- button payForm.accept("Pay","Next")
- if ( button 0 )
- hours payForm.readDouble("hours"
) - pay anEmployee.calculatePay(hour
s) - fillForm(anEmployee)
14The End