cop 3503 fall 2012 shayan javed lecture 13
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/1.jpg)
1 / 431
COP 3503 FALL 2012SHAYAN JAVED
LECTURE 13
Programming Fundamentals using Java
![Page 2: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/2.jpg)
2 / 43
Storage and Retrieval
![Page 3: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/3.jpg)
3 / 43
Storage and Retrieval
Looked at how to read and write files.
![Page 4: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/4.jpg)
4 / 43
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?
![Page 5: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/5.jpg)
5 / 43
Storage and Retrieval
Method 1:
Create a file format
![Page 6: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/6.jpg)
6 / 43
Storage and Retrieval
Method 1:
Create a file format
Store all your info as text
![Page 7: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/7.jpg)
7 / 43
Storage and Retrieval
Example:
Let’s look at the UndergradStudent class from Project 1
![Page 8: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/8.jpg)
8 / 43
Storage and Retrieval
Example:
Let’s look at the UndergradStudent class from Project 1
We want to store all the students
![Page 9: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/9.jpg)
9 / 43
Storage and Retrieval
public class UndergradStudent extends Student {
public UndergradStudent(String name, int UFID, String dob, double gpa) {
super(name, UFID, dob, gpa);}
}
![Page 10: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/10.jpg)
10 / 43
Storage and Retrieval
public class UndergradStudent extends Student {
public UndergradStudent(String name, int UFID, String dob, double gpa) {
super(name, UFID, dob, gpa);}
}
Properties: String nameint UFIDString dobdouble gpa(Ignore courses for now)
![Page 11: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/11.jpg)
11 / 43
Storage
Need to decide on a format to store the info
![Page 12: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/12.jpg)
12 / 43
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
![Page 13: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/13.jpg)
13 / 43
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
![Page 14: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/14.jpg)
14 / 43
Storage
Sample method to write to file:
public static void writeStudent(PrintWriter pw, UndergradStudent student) throws IOException
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()); }
![Page 15: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/15.jpg)
15 / 43
Retrieval
Now we have files with student info in them
![Page 16: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/16.jpg)
16 / 43
Retrieval
Now we have files with student info in them
Need to be able to read the info back and construct student objects
![Page 17: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/17.jpg)
17 / 43
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
![Page 18: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/18.jpg)
18 / 43
Retrievalpublic static UndergradStudent readStudent(Scanner in) throws
IOException {// read line by line// nameString line1 = in.nextLine(); ....// UFIDline1 = in.nextLine(); .....// DOBline1 = in.nextLine(); .....// gpaline1 = in.nextLine(); ....
// Create the student objectUndergradStudent student = new UndergradStudent(name, ufid, dob, gpa);return student;
}
![Page 19: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/19.jpg)
19 / 43
Storage and Retrieval
So we looked at one method of storing and retrieving objects.
![Page 20: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/20.jpg)
20 / 43
Storage and Retrieval
Method 1
Advantages: Stored in human-readable format (open in text editor)
![Page 21: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/21.jpg)
21 / 43
Storage and Retrieval
Method 1
Advantages: Stored in human-readable format (open in text editor) Can edit data directly
![Page 22: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/22.jpg)
22 / 43
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
![Page 23: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/23.jpg)
23 / 43
Storage and Retrieval
Method 1
Disadvantages: Need to write read/write methods
![Page 24: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/24.jpg)
24 / 43
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...)
![Page 25: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/25.jpg)
25 / 43
Storage and Retrieval
There is another way of writing and reading objects.
![Page 26: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/26.jpg)
26 / 43
Storage and Retrieval
There is another way of writing and reading objects.
Create persistent objects in memory.
![Page 27: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/27.jpg)
27 / 43
Storage and Retrieval
There is another way of writing and reading objects.
Create persistent objects in memory.
Objects stored as streams of bytes.
![Page 28: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/28.jpg)
28 / 43
Storage and Retrieval
Objects are serialized (writing)
![Page 29: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/29.jpg)
29 / 43
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.
![Page 30: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/30.jpg)
30 / 43
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)
![Page 31: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/31.jpg)
31 / 43
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)
![Page 32: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/32.jpg)
32 / 43
Serialization
In Java, implement the Serializable interface
![Page 33: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/33.jpg)
33 / 43
Serialization
In Java, implement the Serializable interface
Indicates that it can be converted into a stream of bytes for writing
![Page 34: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/34.jpg)
34 / 43
Serialization
In Java, implement the Serializable interface
Indicates that it can be converted into a stream of bytes for writing
Marker interfaceimport java.io.Serializable;
public className implements Serializable {
![Page 35: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/35.jpg)
35 / 43
Serialization// Create an UndergradStudent objectUndergradStudent student1 = new UndergradStudent("John Smith”, ...)
FileOutputStream fos = null;ObjectOutputStream out = null;try {
// Create FileOutputStreamfos = new FileOutputStream(args[0]); // pass in file nameout = new ObjectOutputStream(fos);// write out the objectsout.writeObject(student1);// close the streamfos.close();
} catch (IOException io) {...}
![Page 36: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/36.jpg)
36 / 43
DeSerialization// Create an UndergradStudent objectUndergradStudent student;
FileInputStream fis = null;ObjectInputStream in = null;try {
// Create FileInputStreamfis = new FileInputStream(args[0]); // pass in file namein = new ObjectInputStream(fis);// read the objectsstudent1 = (UndergradStudent) in.readObject();// close the streamfis.close();
} catch (IOException/ClassNotFoundException io) {...}
![Page 37: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/37.jpg)
37 / 43
Storage and Retrieval
Method 2: Serialization
Advantages: Very simple to write and read. Fast
![Page 38: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/38.jpg)
38 / 43
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).
![Page 39: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/39.jpg)
39 / 43
Storage and Retrieval
What if you don’t want a variable in a class to be written out?
![Page 40: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/40.jpg)
40 / 43
Storage and Retrieval
What if you don’t want a variable in a class to be written out?
public Student implements Serializable {protected Course[] courses;…
}
![Page 41: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/41.jpg)
41 / 43
Storage and Retrieval
What if you don’t want a variable in a class to be written out?
public Student implements Serializable {protected Course[] courses;…
}
We don’t want to store the Courses array when writing out
![Page 42: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/42.jpg)
42 / 43
Storage and Retrieval
What if you don’t want a variable in a class to be written out?
public Student implements Serializable {protected transient Course[] courses;…
}
Use the transient keyword
![Page 43: COP 3503 FALL 2012 Shayan Javed Lecture 13](https://reader035.vdocuments.site/reader035/viewer/2022062315/568161d0550346895dd1c5ad/html5/thumbnails/43.jpg)
43 / 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