Home | Gaming | Programming | Play Online | Contact | Keyword Query
Games++ Games & Game Programming

GAMES++
Games++ Home
Games++ Gaming
Games++ Programming
Beta Testing Games
Free Online Games
Hints & Cheats

BROWSER UTILITIES
E-mail This Page
Add to Favorites

SITE SEARCH

Web Games++

AFFILIATES
Cheat Codes
Trickster Wiki
Game Ratings
Gameboy Cheats
PlayStation Cheats
BlackBerry Games
Photoshop Tutorials
Illustrator Tutorials
ImageReady Tutorials

ADVERTISEMENT

ADVERTISEMENT

How To Serialize An Object

Java How To: Source Code

To serialize an object, it must implement the Serializable interface. The object needs two functions with these signatures.

private void writeObject(java.io.ObjectOutputStream out)
     throws IOException

private void readObject(java.io.ObjectInputStream in)
     throws IOException, ClassNotFoundException;

Many standard Java objects already implement the Serializable interface so there very little that needs to be done.

In the following snippet, we use a Vector to simulate a Queue.

First the Queue state is saved to a file.

Then rename main to main_serialize and main_reload to main and compile. Now the snippet reads the file to reload the Queue from the data previously saved.

import java.util.Vector;
import java.io.*;

public class Queue extends Vector 
{
   /*
   ** FIFO
   */
   Queue() 
   {
      super();
   }
 
   void put(Object o) 
   {
      addElement(o);
   }

   Object get() 
   {
      if (isEmpty()) 
      {
         return null;
      }
    
      Object o = firstElement();
      removeElement(o);

      return o;
   }

   Object peek() 
   {
      if (isEmpty()) 
      {
         return null;
      }

      return firstElement();
   }
    
   public static void main(String args[]) 
   {
      Queue theQueue;
  
      theQueue = new Queue();
      theQueue.put("element 1");
      theQueue.put("element 2");
      theQueue.put("element 3");
      theQueue.put("element 4");
      System.out.println(theQueue.toString());
  
      // serialize the Queue
      System.out.println("serializing theQueue");

      try 
      {
         FileOutputStream fout = new FileOutputStream("thequeue.dat");
         ObjectOutputStream oos = new ObjectOutputStream(fout);
         oos.writeObject(theQueue);
         oos.close();
      }
      catch (Exception e) 
      { 
         e.printStackTrace(); 
      }
   }
  
   public static void main_load(String args[]) 
   {
      Queue theQueue;
    
      theQueue = new Queue();
    
      // unserialize the Queue
      System.out.println("unserializing theQueue");

      try 
      {
         FileInputStream fin = new FileInputStream("thequeue.dat");
         ObjectInputStream ois = new ObjectInputStream(fin);
         theQueue = (Queue) ois.readObject();
         ois.close();
      }
      catch (Exception e) 
      { 
         e.printStackTrace(); 
      }

      System.out.println(theQueue.toString());     
   }
}

How To Serialize an Object over a socket

From the client side, you open a Socket and then

oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(someObject);

On the server side, you use the ObjectInputStream.readObject method.

NOTE: If the OutputStream is kept open and you modify your object and resend it, you will not see a change on the server side. That's because Java keeps objects sent in an internal cache and the old version will be taken in account instead of the new one. The fix is to do an ObjectOutputStream.reset() before re-sending the object or open a new connection each time.

Copyright © 1998-2007, Games++ All rights reserved. | Privacy Policy