Monday, June 6, 2016

Serializing an Object in Java

import java.io.*;

public class Serialize
{
   public static void main(String [] args)
   {
      Employee e = new Employee();
      e.name = "Abc";
      e.address = "india";
      e.SSN = 123;
      e.number = 101;
     
      try
      {
         FileOutputStream fileOut =
         new FileOutputStream("/tmp/employee.ser");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(e);
         out.close();
         fileOut.close();
         System.out.printf("Serialized data is saved in /tmp/employee.ser");
      }catch(IOException i)
      {
          i.printStackTrace();
      }
   }
}

No comments: