430 likes | 509 Vues
COP 3503 FALL 2012 Shayan Javed Lecture 13. Programming Fundamentals using Java. Storage and Retrieval. Storage and Retrieval. Looked at how to read and write files. Storage and Retrieval. Looked at how to read and write files.
E N D
COP 3503 FALL 2012ShayanJavedLecture 13 Programming Fundamentals using Java
Storage and Retrieval • Looked at how to read and write files.
Storage and Retrieval • Looked at how to read and write files. • How can we store and later retrieve the objects we need for our programs?
Storage and Retrieval • Method 1: • Create a file format
Storage and Retrieval • Method 1: • Create a file format • Store all your info as text
Storage and Retrieval • Example: • Let’s look at the UndergradStudent class from Project 1
Storage and Retrieval • Example: • Let’s look at the UndergradStudent class from Project 1 • We want to store all the students
Storage and Retrieval public class UndergradStudentextends Student { publicUndergradStudent(String name, int UFID, String dob, double gpa) { super(name, UFID, dob, gpa); } }
Storage and Retrieval public class UndergradStudentextends Student { publicUndergradStudent(String name, int UFID, String dob, double gpa) { super(name, UFID, dob, gpa); } } Properties: String name intUFID String dob double gpa (Ignore courses for now)
Storage • Need to decide on a format to store the info
Storage • Need to decide on a format to store the info • Sample format: Undergrad Student: Name: name UFID: UFID D.O.B: dob GPA: gpa
Storage • Need to decide on a format to store the info • Sample format: Undergrad Student: Name: name UFID: UFID D.O.B: dob GPA: gpa
Storage • Sample method to write to file: publicstaticvoidwriteStudent(PrintWriter pw, UndergradStudent student) throwsIOException pw.println("Undergrad Student: "); pw.println("Name: " + student.getName()); pw.println("UFID: " + student.getUFID()); pw.println("D.O.B: " + student.getDob()); pw.println("GPA: " + student.getGpa()); }
Retrieval • Now we have files with student info in them
Retrieval • Now we have files with student info in them • Need to be able to read the info back and construct student objects
Retrieval • Now we have files with student info in them • Need to be able to read the info back and construct student objects • Have to parse the file
Retrieval publicstaticUndergradStudentreadStudent(Scanner in) throwsIOException { // read line by line // name String line1 = in.nextLine(); .... // UFID line1 = in.nextLine(); ..... // DOB line1 = in.nextLine(); ..... // gpa line1 = in.nextLine(); .... // Create the student object UndergradStudent student = newUndergradStudent(name, ufid, dob, gpa); return student; }
Storage and Retrieval • So we looked at one method of storing and retrieving objects.
Storage and Retrieval • Method 1 • Advantages: • Stored in human-readable format (open in text editor)
Storage and Retrieval • Method 1 • Advantages: • Stored in human-readable format (open in text editor) • Can edit data directly
Storage and Retrieval • Method 1 • Advantages: • Stored in human-readable format (open in text editor) • Can edit data directly • Can be read in any programming language
Storage and Retrieval • Method 1 • Disadvantages: • Need to write read/write methods
Storage and Retrieval • Method 1 • Disadvantages: • Need to write read/write methods • Reading/Writing can be slow • (Create Scanner, read next line, then next token, etc. etc...)
Storage and Retrieval • There is another way of writing and reading objects.
Storage and Retrieval • There is another way of writing and reading objects. • Create persistent objects in memory.
Storage and Retrieval • There is another way of writing and reading objects. • Create persistent objects in memory. • Objects stored as streams of bytes.
Storage and Retrieval • Objects are serialized (writing)
Storage and Retrieval • Objects are serialized (writing) Serialization is the process of converting an object state into a format that can be stored (for example, in a file or memory buffer) and "resurrected" later in the same or another computer environment.
Storage and Retrieval • Objects are serialized (writing) Serialization is the process of converting an object state into a format that can be stored (for example, in a file or memory buffer) and "resurrected" later in the same or another computer environment. • “Resurrection” = Deserialization (reading)
Storage and Retrieval • Objects are serialized (writing) Serialization is the process of converting an object state into a format that can be stored (for example, in a file or memory buffer) and "resurrected" later in the same or another computer environment. • “Resurrection” = Deserialization (reading)
Serialization • In Java, implement the Serializable interface
Serialization • In Java, implement the Serializable interface • Indicates that it can be converted into a stream of bytes for writing
Serialization • In Java, implement the Serializable interface • Indicates that it can be converted into a stream of bytes for writing • Marker interface importjava.io.Serializable; publicclassNameimplementsSerializable {
Serialization // Create an UndergradStudent object UndergradStudent student1 = new UndergradStudent("John Smith”, ...) FileOutputStreamfos = null; ObjectOutputStream out = null; try { // Create FileOutputStream fos = newFileOutputStream(args[0]); // pass in file name out = newObjectOutputStream(fos); // write out the objects out.writeObject(student1); // close the stream fos.close(); } catch (IOExceptionio) {...}
DeSerialization // Create an UndergradStudent object UndergradStudent student; FileInputStreamfis = null; ObjectInputStream in = null; try { // Create FileInputStream fis = newFileInputStream(args[0]); // pass in file name in = newObjectInputStream(fis); // read the objects student1 = (UndergradStudent) in.readObject(); // close the stream fis.close(); } catch (IOException/ClassNotFoundExceptionio) {...}
Storage and Retrieval • Method 2: Serialization • Advantages: • Very simple to write and read. • Fast
Storage and Retrieval • Method 2: Serialization • Disadvantages: • Not human-readable. • Cannot be edited by text-editors • Restricted to one programming language (not exactly…have to implement reading/writing algorithm for other languages).
Storage and Retrieval • What if you don’t want a variable in a class to be written out?
Storage and Retrieval • What if you don’t want a variable in a class to be written out? publicStudent implementsSerializable{ protected Course[] courses; … }
Storage and Retrieval • What if you don’t want a variable in a class to be written out? publicStudent implementsSerializable{ protected Course[] courses; … } We don’t want to store the Courses array when writing out
Storage and Retrieval • What if you don’t want a variable in a class to be written out? publicStudent implementsSerializable{ protectedtransient Course[] courses; … } Use the transient keyword
Summary • Two ways of storage/retrieval: • Text files • specify a file format, write parsers • Serializable • stream of bytes • write out objects directly, read back directly TODO: Look into “JSON” as a format