Anna University Results November - December 2011 available with your GPA only for Credit System

Exam Results 2012

Exam Results 2012 Inspirational Quotes

Saturday, September 25, 2010

Object Serialization in Java


 /* SerializationWrite.java */

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

class Currency implements Serializable
{
  protected String currency;
  protected int amount;
 
  public Currency(String cur, int amt)
  {
    this.currency = cur;
    this.amount = amt;
  }
  public String toString()
  {
    return currency + amount;
  }
  public String dollarToRupee(int amt)
  {
    return "Rs" + amt * 45;
  }
}
class Rupee extends Currency
{
  public Rupee(int amt)
  {
    super("Rs",amt);
  }
}
class Dollar extends Currency
{
  public Dollar(int amt)
  {
    super("$",amt);
  }
}
public class SerializationWrite
{
  public static void main(String args[])
  {
    Random r = new Random();
    try
    {
      Currency currency;
      FileOutputStream fos = new FileOutputStream("serial.txt");
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      System.out.println("Writing to file using Object Serialization:");
      for(int i=1;i<=25;i++)
      {
        Object[] obj = { new Rupee(r.nextInt(5000)),new Dollar(r.nextInt(5000)) };
        currency = (Currency)obj[r.nextInt(2)]; // RANDOM Objects for Rs and $
        System.out.println(currency);
        oos.writeObject(currency);
        oos.flush();
      }
      oos.close();
    }
    catch(Exception e)
    {
      System.out.println("Exception during Serialization: " + e);
    }
  }
}

OUTPUT :

Writing to file using Object Serialization:

Rs2337 
Rs1629 
$339 
Rs1092
Rs1193
$1957
Rs1450 
$1799
$1497
$4586 
Rs1650
Rs88 
Rs1862 
$4392
$4216 
Rs4722
Rs676
Rs3159
$409
$1093
Rs2751
Rs2299
Rs1595
$4650
Rs2126

/* SerializationRead.java */

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

public class SerializationRead
{
  public static void main(String args[])
  {
    try
    {
      Currency obj;
      FileInputStream fis = new FileInputStream("serial.txt");
      ObjectInputStream ois = new ObjectInputStream(fis);
      System.out.println("Reading from file using Object Serialization:");
      for(int i=1;i<=25;i++)
      {
        obj = (Currency)ois.readObject();
        if((obj.currency).equals("$"))
          System.out.println(obj + " = " + obj.dollarToRupee(obj.amount));
        else
          System.out.println(obj + " = " + obj);
         
      }
      ois.close();
    }
    catch(Exception e)
    {
      System.out.println("Exception during deserialization." + e);
    }
  }


OUTPUT :

Reading from file using Object Serialization:

Rs2337 = Rs2337
Rs1629 = Rs1629
$339 = Rs15255
Rs1092 = Rs1092
Rs1193 = Rs1193
$1957 = Rs88065
Rs1450 = Rs1450
$1799 = Rs80955
$1497 = Rs67365
$4586 = Rs206370
Rs1650 = Rs1650
Rs88 = Rs88
Rs1862 = Rs1862
$4392 = Rs197640
$4216 = Rs189720
Rs4722 = Rs4722
Rs676 = Rs676
Rs3159 = Rs3159
$409 = Rs18405
$1093 = Rs49185
Rs2751 = Rs2751
Rs2299 = Rs2299
Rs1595 = Rs1595
$4650 = Rs209250
Rs2126 = Rs2126

If  u find any flaws , Contact to M.Baran Mahamood

No comments: