COP 3503 FALL 2012 Shayan Javed Lecture 13

Post on 22-Feb-2016

23 views 1 download

Tags:

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 Presentation

transcript

1 / 431

COP 3503 FALL 2012SHAYAN JAVED

LECTURE 13

Programming Fundamentals using Java

2 / 43

Storage and Retrieval

3 / 43

Storage and Retrieval

Looked at how to read and write files.

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?

5 / 43

Storage and Retrieval

Method 1:

Create a file format

6 / 43

Storage and Retrieval

Method 1:

Create a file format

Store all your info as text

7 / 43

Storage and Retrieval

Example:

Let’s look at the UndergradStudent class from Project 1

8 / 43

Storage and Retrieval

Example:

Let’s look at the UndergradStudent class from Project 1

We want to store all the students

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);}

}

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)

11 / 43

Storage

Need to decide on a format to store the info

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

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

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

15 / 43

Retrieval

Now we have files with student info in them

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

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

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;

}

19 / 43

Storage and Retrieval

So we looked at one method of storing and retrieving objects.

20 / 43

Storage and Retrieval

Method 1

Advantages: Stored in human-readable format (open in text editor)

21 / 43

Storage and Retrieval

Method 1

Advantages: Stored in human-readable format (open in text editor) Can edit data directly

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

23 / 43

Storage and Retrieval

Method 1

Disadvantages: Need to write read/write methods

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

25 / 43

Storage and Retrieval

There is another way of writing and reading objects.

26 / 43

Storage and Retrieval

There is another way of writing and reading objects.

Create persistent objects in memory.

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.

28 / 43

Storage and Retrieval

Objects are serialized (writing)

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.

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)

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)

32 / 43

Serialization

In Java, implement the Serializable interface

33 / 43

Serialization

In Java, implement the Serializable interface

Indicates that it can be converted into a stream of bytes for writing

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 {

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

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

37 / 43

Storage and Retrieval

Method 2: Serialization

Advantages: Very simple to write and read. Fast

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

39 / 43

Storage and Retrieval

What if you don’t want a variable in a class to be written out?

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;…

}

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

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

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