1 / 43

COP 3503 FALL 2012 Shayan Javed Lecture 13

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.

conway
Download Presentation

COP 3503 FALL 2012 Shayan Javed Lecture 13

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COP 3503 FALL 2012ShayanJavedLecture 13 Programming Fundamentals using Java

  2. Storage and Retrieval

  3. Storage and Retrieval • Looked at how to read and write files.

  4. 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?

  5. Storage and Retrieval • Method 1: • Create a file format

  6. Storage and Retrieval • Method 1: • Create a file format • Store all your info as text

  7. Storage and Retrieval • Example: • Let’s look at the UndergradStudent class from Project 1

  8. Storage and Retrieval • Example: • Let’s look at the UndergradStudent class from Project 1 • We want to store all the students

  9. Storage and Retrieval public class UndergradStudentextends Student { publicUndergradStudent(String name, int UFID, String dob, double gpa) { super(name, UFID, dob, gpa); } }

  10. 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)

  11. Storage • Need to decide on a format to store the info

  12. 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

  13. 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

  14. 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()); }

  15. Retrieval • Now we have files with student info in them

  16. Retrieval • Now we have files with student info in them • Need to be able to read the info back and construct student objects

  17. 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

  18. 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; }

  19. Storage and Retrieval • So we looked at one method of storing and retrieving objects.

  20. Storage and Retrieval • Method 1 • Advantages: • Stored in human-readable format (open in text editor)

  21. Storage and Retrieval • Method 1 • Advantages: • Stored in human-readable format (open in text editor) • Can edit data directly

  22. 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

  23. Storage and Retrieval • Method 1 • Disadvantages: • Need to write read/write methods

  24. 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...)

  25. Storage and Retrieval • There is another way of writing and reading objects.

  26. Storage and Retrieval • There is another way of writing and reading objects. • Create persistent objects in memory.

  27. Storage and Retrieval • There is another way of writing and reading objects. • Create persistent objects in memory. • Objects stored as streams of bytes.

  28. Storage and Retrieval • Objects are serialized (writing)

  29. 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.

  30. 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)

  31. 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)

  32. Serialization • In Java, implement the Serializable interface

  33. Serialization • In Java, implement the Serializable interface • Indicates that it can be converted into a stream of bytes for writing

  34. 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 {

  35. 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) {...}

  36. 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) {...}

  37. Storage and Retrieval • Method 2: Serialization • Advantages: • Very simple to write and read. • Fast

  38. 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).

  39. Storage and Retrieval • What if you don’t want a variable in a class to be written out?

  40. Storage and Retrieval • What if you don’t want a variable in a class to be written out? publicStudent implementsSerializable{ protected Course[] courses; … }

  41. 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

  42. 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

  43. 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

More Related