quest4tech.net

 

Example of Java Serialization

The Java files in the following sections give an example of creating and serializing an object and then deserializing it.
  • Class for Radio - Radio.java, a class file to make object Radio.
     
  • Serialize an object created from Class Radio - SerializeDemo.java, creates the object class Radio, assigns values to its fields and writes a serial object file, radio.ser.
     
  • Deserialize the saved object created from class Radio - DeserializeDemo.java, reads the serial object file, radio.ser, deserializes it and prints out its values.

Class for Radio

// Radio.java
import java.util.*;
 
public class Radio implements java.io.Serializable
{
    public String colour;
    public float station;
    public int volume;
 
    public void setVolume(int v)
    {
        volume = v * 2;
    }
}
 

Serialize an object created from Class Radio

// SerializeDemo.java
import java.io.*;
 
public class SerializeDemo
{
    public static void main(String [] args)
    {
        Radio radio = new Radio();
        radio.colour = "Red";
        radio.station = 100.3F;
        radio.setVolume(10);
        try
        {
            FileOutputStream fileOut = new FileOutputStream("radio.ser");
            ObjectOutputStream serOut = new ObjectOutputStream(fileOut);
            serOut.writeObject(radio);
            fileOut.close();
            serOut.close();
        }catch(IOException e)
        {
            System.out.println("IOException");
            e.printStackTrace();
        }
    }
}
 

Deserialize the saved object created from class Radio

// DeserializeDemo.java
import java.io.*;
 
public class DeserializeDemo
{
    public static void main(String [] args)
    {
        Radio r = null;
        try
        {
            FileInputStream fileIn = new FileInputStream("radio.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
 
            r = (Radio) in.readObject();
 
            in.close();
            fileIn.close();
 
        } catch(IOException e)
        {
            System.out.println("IOException");
            e.printStackTrace();
            return;
        } catch(ClassNotFoundException f)
        {
            System.out.println("Radio class not found");
            f.printStackTrace();
            return;
        }
 
        System.out.println("\nJust deserialized Radio...");
        System.out.println("Colour: " + r.colour);
        System.out.println("Station: " + r.station);
        System.out.println("Volume " + r.volume);
        System.out.println("");
    }
}
 

Running the example

For the example the files were placed in the same directory and the following batch files were used to serialize and then deserialize the object Radio.
 

Serialize the object Radio

echo off
rem SerializeDemo.bat
del SerializeDemo.class
javac SerializeDemo.java
java SerializeDemo
pause
echo on

Deserialize the saved object Radio

echo off
rem DeserializeDemo.bat
del DeserializeDemo.class
javac DeserializeDemo.java
java DeserializeDemo
pause
echo on



To add an entry, request an existing one to be altered or make a comment please click here.

quest4tech.net
 

(c) Compiled by B V Wood.


 

 

 Main Index
 

USA SPONSORS