Archive for the ‘Java.io.Serializable’ Category

PostHeaderIcon Java Serialization with Inheritance 1

This example shows a simple example of Serialization with Inheritance, where the subclass implements Serializable but the superclass does not. Please note that in order to run the program you will have to either: a) create the following folder on the Windows file system: E:\\Java\scjp\Creating Files\Serialization or b) Change line 23 of SerializationTest to reflect your filesystem i.e. where you want to store the file. The following code is written using good Object Oriented design i.e. using private visibility modifiers along with getters and setters for the instance variables. Compare this to the output in the article: Java Serialization 2. Notice that Person.java does not implement Serializable (like with the article: Java Serialization with Inheritance 2 and also: Java Serialization with Inheritance 3). The reason all the values inherited by Student from Person appear after serialization is because although a reference to Person is created in SerializationTest, an instance of Sudent is actually created i.e. Person p = new Student().

//Person.java*****

public class Person //Person could be Faculty or Student in this case
{
private String name;
private int age;
private String address;
private String telephoneNumber;

public Person (String name2, int age2, String address2, String telephoneNumber2)
{
System.out.println(“Person Constructor called with the following arguments: “+name2+” “+age2+” “+address2+” “+telephoneNumber2);
name = name2;
age = age2;
address = address2;
telephoneNumber = telephoneNumber2;
}

public Person()
{
System.out.println(“Person constructor called with no arguments”);
name = “”;
age = 0;
address=”";
telephoneNumber=”";
}

public String getName ()
{
return this.name;
}

public void setTelephoneNumber(String telephoneNumber2)
{
telephoneNumber = telephoneNumber2;
}

public String getTelephoneNumber()
{
return telephoneNumber;
}

public int getAge()
{
return age;
}

public String getAddress()
{
return address;
}
}

//Student.java

import java.io.*;

public class Student extends Person implements Serializable
{
private int studentID; //Assuming StudentID is an int
private String course;

public Student (int studentID2, String course2, String name2, int age2, String address2, String telephoneNumber2)
{
super(name2, age2, address2, telephoneNumber2);
System.out.println(“Student constructor called with the following values: “+studentID2+” “+course2+” “+name2+” “+age2+” “+address2+” “+telephoneNumber2);
studentID = studentID2;
course = course2;
}

public Student()
{
super();
System.out.println(“Constructor called with no arguents”);
studentID = 0;
course = “”;
}

public void setCourse(String course2)
{
course = course2;
}

public String getCourse()
{
return course;
}

public int getStudentID()
{
return studentID;
}
}

//SerializationTest.java

PostHeaderIcon Java Serialization with Inheritance 3

In this example their are no getter or setter methods i.e. the instance variables are accessed directly from SerializationTest.java. WriteObject() and ReadObject() are implemented in Student.java (to serialize the Person instance variables). Notice now all the variable values appear after deserialization completes in SerializationTest().

//Person.java

public class Person //Person could be Faculty or Student in this case
{
public String name;
public int age;
public String address;
public String telephoneNumber;

public Person()
{
System.out.println(“Person constructor invoked with: “+name+” “+age+” “+address+” ” +telephoneNumber);
name = “”;
age = 0;
address=”";
telephoneNumber=”";
}
}

//Student.java

import java.io.*;

public class Student extends Person implements Serializable
{
public int studentID; //Assuming StudentID is an int
public String course;

public Student()
{
super();
System.out.println(“Student constructor invoked with: “+studentID+ ” “+course);
studentID = 0;
course = “”;
}

//readobject and writeobject can be declared private because it is the JVM that checks if they are declared and not the object
private void writeObject(ObjectOutputStream oos)
{
try
{
oos.defaultWriteObject();
oos.writeObject(name);
oos.writeInt(age);
oos.writeObject(address);
oos.writeObject(telephoneNumber);

}
catch (Exception e)
{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bout));
System.out.println(bout.toString());
}
}

private void readObject(ObjectInputStream ois)
{
try
{
ois.defaultReadObject();
name = (String)ois.readObject();
age = ois.readInt();
address = (String)ois.readObject();
telephoneNumber = (String)ois.readObject();
}
catch (Exception e)
{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bout));
System.out.println(bout.toString());
}
}
}

//SerializationTest.java

import java.io.*;

public class SerializationTest
{
public static void main (String[] args)
{
try
{
int StudentID = 1;
Student s = new Student();

s.studentID = 1;
s.course = “Business Information Systems”;
s.name = “Ian Stanford”;
s.address = “Lincoln”;
s.age = 28;
s.telephoneNumber = “01522223344″;
System.out.println(“Values before Serialization:”);
System.out.println(“Student ID: “+s.studentID);
System.out.println(“Course: “+s.course);
System.out.println(“Name: “+s.name);
System.out.println(“Age: “+s.age);
System.out.println(“Address: “+s.address);
System.out.println(“Telephone Number: “+s.telephoneNumber);

File directory = new File(“E:\\Java\\scjp\\Creating Files\\SerializationInheritance”);
String StudentIDString = Integer.toString(StudentID);
String filename = StudentIDString+”.ser”;
System.out.println(“Serialization filename is: “+filename);
File f = new File (directory, filename);
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(s);

FileInputStream fis = new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream(fis);

//s=null;
//p=null;

s =(Student)ois.readObject();
ois.close();

//Print Values to screen
System.out.println(“”);
System.out.println(“Values after deserialization”);
System.out.println(“StudentID = ” +s.studentID);
System.out.println(“Course = ” +s.course);
System.out.println(“Name = ” +s.name);
System.out.println(“Age = ” +s.age);
System.out.println(“Address = ” +s.address);
System.out.println(“Telephone Number = ” +s.telephoneNumber);

}
catch (Exception e)
{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bout));
System.out.println(bout.toString());
}

}
}

PostHeaderIcon Java Serialization with Inheritance 2

This example shows a Serialization example where readObject() and writeObject() are not implemented in the Student class i.e. the Person instance variables are not serialised with the Student object. See Java Serialization with Inheritance 3 for an example where the Person instance variables are serialised and deserialised with the Student object.

//Person.java

public class Person //Person could be Faculty or Student in this case
{
public String name;
public int age;
public String address;
public String telephoneNumber;

public Person()
{
System.out.println(“Person Constructor called”);
name = “”;
age = 0;
address=”";
telephoneNumber=”";
}
}

//Student.java

import java.io.*;

public class Student extends Person implements Serializable
{
public int studentID; //Assuming StudentID is an int
public String course;

public Student()
{
super();
System.out.println(“Student Constructor called”);
studentID = 0;
course = “”;
}
}

//SerializationTest.java

import java.io.*;

public class SerializationTest
{
public static void main (String[] args)
{
try
{
int StudentID = 1;
Student s = new Student();

s.studentID = 1;
s.course = “Business Information Systems”;
s.name = “Ian Stanford”;
s.address = “Lincoln”;
s.telephoneNumber = “01522223344″;
System.out.println(“Values before Serialization:”);
System.out.println(“Student ID: “+s.studentID);
System.out.println(“Course: “+s.course);
System.out.println(“Name: “+s.name);
System.out.println(“Age: “+s.age);
System.out.println(“Address: “+s.address);
System.out.println(“Telephone Number: “+s.telephoneNumber);

File directory = new File(“E:\\Java\\scjp\\Creating Files\\SerializationNoAccessors”);
String StudentIDString = Integer.toString(StudentID);
String filename = StudentIDString+”.ser”;
System.out.println(“Serialization filename is: “+filename);
File f = new File (directory, filename);
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(s);

FileInputStream fis = new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream(fis);

//s=null;
//p=null;

s =(Student)ois.readObject();
ois.close();

//Print Values to screen
System.out.println(“”);
System.out.println(“Values after deserialization”);
System.out.println(“StudentID = ” +s.studentID);
System.out.println(“Course = ” +s.course);
System.out.println(“Name = ” +s.name);
System.out.println(“Age = ” +s.age);
System.out.println(“Address = ” +s.address);
System.out.println(“Telephone Number = ” +s.telephoneNumber);

}
catch (Exception e)
{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bout));
System.out.println(bout.toString());
}
}
}

PostHeaderIcon Java Serializing a class with an Enum type

//readResolve() is handled automatically
import java.io.*;

public class Today implements Serializable
{
Day day;
String openingHours;
String doctorName;

public Today(Day day, String openingHours, String doctorName)
{
this.day = day;
this.openingHours = openingHours;
this.doctorName = doctorName;
}

public String toString()
{
return day + ” ” +openingHours+ ” ” +doctorName;
}

public static void main (String [] args)
{
try
{
Today d1 = new Today(Day.MONDAY, “12:00-14:00″, “Dr Smith”);
File directory = new File (“E:\\Java\\scjp\\Enum\\Enum Serialization”);
File file = new File (directory, “EnumTest.ser”);
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(d1);
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Today d2 = (Today)ois.readObject();
ois.close();
oos.close();
System.out.println(d2.toString());
}
catch (Exception e)
{
System.out.println(“There was an exception: “+e.toString());
}
}

}

PostHeaderIcon Java Singleton Serialization

import java.io.*;

public class SingletonStudent implements Serializable
{
private int studentID; //Assuming StudentID is an int
private String course;
private static String StaticTest;

public static final SingletonStudent Instance = new SingletonStudent(1, “Business Information Systems”); //final means it is a constant, therefore multiple calls aren’t allowed with changing the value

public static SingletonStudent getInstance()
{
return Instance;
}

private SingletonStudent()
{
System.out.println(“Constructor called with no arguents”);
studentID = 0;
course = “”;
}

private SingletonStudent (int studentID2, String course2)
{
System.out.println(“Student constructor called with the following values: “+studentID2+” “+course2);
studentID = studentID2;
course = course2;
}

public void setCourse(String course2)
{
course = course2;
}

public String getCourse()
{
return course;
}

public int getStudentID()
{
return studentID;
}

public Object readResolve() throws ObjectStreamException
{
return Instance;
}

static
{
System.out.println(“Static Initialiser run”);
StaticTest = “Test”;
}

public static void main (String args[])
{
System.out.println(“Main method running”);
SingletonStudent s1 = SingletonStudent.getInstance();
SingletonStudent s2 = SingletonStudent.getInstance();
if (s1 == s2)
{
System.out.println(“this is a Singleton class so this is always true!”);
}
System.out.println(s1.getCourse());
System.out.println(s2.getCourse());
s1.setCourse(“Computing Science”);
System.out.println(s1.getCourse());
System.out.println(s2.getCourse());
try
{
File directory = new File(“c:\\”);
File file = new File (directory, “test.ser”);
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(s1); //eof exception thrown if this is omitted.
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
SingletonStudent s =(SingletonStudent)ois.readObject();
//ois.readObject();
System.out.println(“After de-serialization”);
System.out.println(s.getCourse());
System.out.println(s.getCourse());
s.setCourse(“Business Information Systems”);

//**************s, s1 and s2 are all “Business Information Systems” if readresolve() is used. If readresolve is commented out then s1 and s2 display “Computing Science”
System.out.println(s1.getCourse());
System.out.println(s2.getCourse());
System.out.println(s.getCourse());
//*********************************************************************************
ois.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}

Adsense