Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 2008 Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 20.

Similar presentations


Presentation on theme: "Spring 2008 Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 20."— Presentation transcript:

1 Spring 2008 Mark Fontenot mfonten@engr.smu.edu CSE 1341 - Honors Principles of Computer Science I Note Set 20

2 Note Set 20 Overview Object Serialization Object Deserialization

3 Types of Files Text File Store a text/string representation of the data regardless of its original type 1,332,443,543 requires 10 bytes of data to be stored as text requires only 4 bytes of data to be stored as binary data Binary Data Lower level representation of data Allows us to write data to a file in exact same form as it is stored in memory (usually….)

4 Streams Your Program Running Your Program Running Destination Stream of bytes For us, this will be a file. But it could be a network connect, etc. Abstraction

5 Stream Classes in Java java.io.FileOutputStream Byte-level writing to file java.io.FileInputStream Byte-level reading from a file java.io.ObjectOutputStream Write a complete object to an output stream java.io.ObjectInputStream Read a complete object from an output stream

6 Serialization Serialized object object represented as a sequence of bytes includes object’s data, object’s type and information about the type of data stored in the object itself. Use writeObject(…) of ObjectOutputStream De-serialization reading a sequence of bytes from a file and converting them back into an object Will read in a generic object of type Object use readObject(…) of ObjectInputStream

7 Serializable Interface Class must implement Serializable in order to be serialized import java.io.*; public class Student implements Serializable { private String name; private String id; private double gpa; public Student () {} public Student (String n, String i, double g) { name = n; id = i; gpa = g; } Programmer’s responsibility to ensure that all data members of a serializable class are themselves serializable. Primitives are always serializable.

8 Create Binary File import java.io.*; public class CreateBinaryFile { public static void main (String [] args) { //Create some sample student objects Student s1 = new Student ("Sam", "12334321", 3.2); Student s2 = new Student ("Sally", "33445566", 3.3); Student s3 = new Student ("John", "22446688", 3.4);

9 Create Binary File try { /** * We use object output stream to write entire objects to * a stream (a file in this case). The object output stream * can write to different types of streams, so we create a file * output stream and use that to construct our object output stream */ FileOutputStream fOut = new FileOutputStream("Students.dat"); ObjectOutputStream output = new ObjectOutputStream(fOut); //Use the writeObject method to send a stream of bytes representing //our objects to the file output.writeObject(s1); output.writeObject(s2); output.writeObject(s3); //Don't forget to close the file. output.close(); }catch (IOException e) { System.out.println(e.getMessage()); System.exit(1); } System.out.println("Done writing..."); }

10 Reading the Binary File import java.io.*; import java.util.*; public class ReadBinaryFile { public static void main (String [] args) { //In this case, the stream object needs to be declared outside //the try..catch because of the way we are reading from the file. ObjectInputStream in = null; //We will store our objects in an ArrayList. We could store them //in an array, but then we'd have to knwo how many objects were in the //file in order to declare the array. ArrayList list = new ArrayList ();

11 Reading the Binary File try { //Open the file input stream and send it to the constructor //of object input stream FileInputStream file = new FileInputStream("Students.dat"); in = new ObjectInputStream(file); //Read until an end of file exception is thrown... while (true) { //When we read from the object input stream, the readObject //method returns type Object (the super class of all superclasses) //We know it is a student, so we can explicity cast it to a //Student type sot hat it can be stored in a student reference //variable. Student temp = (Student) in.readObject(); //Add the student object read to the array list list.add(temp); } catch(EOFException e) { //This excpetion will be thrown when ever we get to the end of the //file. System.out.println("Done Reading..."); }

12 Reading the Binary Data catch (ClassNotFoundException e) { System.out.println("ERROR: " + e.getMessage()); } catch (IOException e) { System.out.println("ERROR: " + e.getMessage()); } //This try..catch is to close the file. try { in.close(); } catch (IOException e) { System.out.println(e.getMessage()); } //Proof of concept. System.out.println("Here is what was in the file..."); for(Student x : list) { System.out.printf("%-10s %-10s %.2f\n", x.getName(), x.getId(), x.getGpa()); }


Download ppt "Spring 2008 Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 20."

Similar presentations


Ads by Google