Java Serializable Simple Example


Java Serializable Simple Example 


Some basic code to demonstrate the power of serialization and deserialization  of an object.

Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.

After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.


Most impressive is that the entire process is JVM independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform.

Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object

start.java


class start
{
public static void main(String args[])
{
//CREATE INSTANCE OF CLASSES BELOW
//CUSTOMER CLASS - SOME SETTERS AND GETtERS TO ACCESS PRIVATE VARIABLE ARRAY

customer cust = new customer();
seridemo seri = new seridemo();
deseridemo deseri = new deseridemo();


//POPULATE CUSTOMER CLASS ARRAY WITH SOME DATA
cust.setStuff("Jim","Jim@hotmail.com",0);
cust.setStuff("Paul","Paul@hotmail.com",1);
cust.setStuff("Jane","Jane@hotmail.com",2);
cust.setStuff("Clair","Clair@hotmail.com",3);
cust.setStuff("Andy","Andy@hotmail.com",4);

//PASS THE CUST OBJECT TO A CLASS TO WRITE THE OBJECT TO FILE
seri.seridemox(cust);


// CALL CLASS TO READ OBJECT FROM FILE 
for(int counter=0; counter<5; counter++)
{
System.out.printf("NAME  : %s  ---  ",deseri.readObj().getName(counter));
System.out.printf("EMAIL : %s\n",deseri.readObj().getEmail(counter));
}
}
}




The ObjectOutputStream class is used to serialize an Object.

When the program is done executing, a file named  customer.ser is created. The program does not generate any output, but study the code and try to determine what the program is doing.

seridemo.java



import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;

class seridemo
{
public void seridemox(customer x)
{
try
{
FileOutputStream fileOut = new FileOutputStream("customer.ser");
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
objOut.writeObject(x);
objOut.close();
fileOut.close();
}
catch(IOException i)
{
i.printStackTrace();
}
}
}



The following deseridemo.java  program deserializes the customer object created in the  seridemo.java program. 

deseridemo.java



import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.IOException;


class deseridemo
{
public customer readObj()
{
customer cust = null;

try
{
FileInputStream fileIn = new FileInputStream("customer.ser");
ObjectInputStream objIn = new ObjectInputStream(fileIn);
cust=(customer) objIn.readObject();

fileIn.close();
objIn.close();

}
catch(IOException i)
      {
        i.printStackTrace();
      }
      catch(ClassNotFoundException c)
      {
        System.out.println("Employee class not found.");
          c.printStackTrace();
      }
      return cust;
}
}

customer.java



import java.io.Serializable;

class customer implements Serializable
{
private String name[] = new String[5];
private String email[] = new String[5];

public void setStuff(String newName, String newEmail, int pos)
{
name[pos]=newName;
email[pos]=newEmail;
}

public String getName(int x)
{
return name[x];
}

public String getEmail(int x)
{
return email[x];
}
}



No comments:

Post a Comment