Binary%20Files%20 - PowerPoint PPT Presentation

About This Presentation
Title:

Binary%20Files%20

Description:

Data Structures and Abstraction. 2.4. Record Output. object I/O to binary streams only ... Created at compile time as a computed hash of various class elements: ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 9
Provided by: hug276
Category:

less

Transcript and Presenter's Notes

Title: Binary%20Files%20


1
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
2
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

3
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

4
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

5
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

6
Record Input
  • readObject()
  • must downcast to expected type
  • not a conversion
  • ClassCastException
  • Matches serial number (SUID) of object to
    objects class specification
  • Security
  • Class Versioning.

7
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
  • Employees are not new
  • persistent objects
  • serialization
  • same class file
  • can recompile Employee class
  • If explicit serialVersionUID is used
  • PayRoll with persistent objects

8
PayRoll 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)
10
Employee 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.
    /

11
EmployeeCreate
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()

12
Employee 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()

13
PayRoll
  • 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)

14
The End
Write a Comment
User Comments (0)
About PowerShow.com