+ All Categories
Transcript
Page 1: COP  3503  FALL 2012 Shayan Javed Lecture 13

1 / 431

COP 3503 FALL 2012SHAYAN JAVED

LECTURE 13

Programming Fundamentals using Java

Page 2: COP  3503  FALL 2012 Shayan Javed Lecture 13

2 / 43

Storage and Retrieval

Page 3: COP  3503  FALL 2012 Shayan Javed Lecture 13

3 / 43

Storage and Retrieval

Looked at how to read and write files.

Page 4: COP  3503  FALL 2012 Shayan Javed Lecture 13

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

5 / 43

Storage and Retrieval

Method 1:

Create a file format

Page 6: COP  3503  FALL 2012 Shayan Javed Lecture 13

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

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

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

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

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

11 / 43

Storage

Need to decide on a format to store the info

Page 12: COP  3503  FALL 2012 Shayan Javed Lecture 13

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

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

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

15 / 43

Retrieval

Now we have files with student info in them

Page 16: COP  3503  FALL 2012 Shayan Javed Lecture 13

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

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

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

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

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

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

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

23 / 43

Storage and Retrieval

Method 1

Disadvantages: Need to write read/write methods

Page 24: COP  3503  FALL 2012 Shayan Javed Lecture 13

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

25 / 43

Storage and Retrieval

There is another way of writing and reading objects.

Page 26: COP  3503  FALL 2012 Shayan Javed Lecture 13

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

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

28 / 43

Storage and Retrieval

Objects are serialized (writing)

Page 29: COP  3503  FALL 2012 Shayan Javed Lecture 13

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

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

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

32 / 43

Serialization

In Java, implement the Serializable interface

Page 33: COP  3503  FALL 2012 Shayan Javed Lecture 13

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

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

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

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

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

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

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

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

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

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

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


Top Related