JAVA APPLICATION - SERIALIZATION

AIM:
    To write a Java program to implement serialization.

ALGORITHM:
Step 1: Start
Step 2: Define the class MyInteger which implements serializable
Step 3: Define the class TestSerial
Step 4: Declare the objects for the class MyInteger
Step 5: Define the constructor TestSerial
Step 6: Initialize the object i1
Step 7: Perform the try catch operation for ObjectOutputStream
Step 8: Perform the try catch operation for ObjectInputStream
Step 9: Initialize the object for the class TestSerial
Step 10: Stop

SOURCE CODE:
TestSerial.java
import java.io.*;
class MyInteger implements Serializable
{

private Integer integer;
public MyInteger(int i)
{
integer=new Integer(i);
}

public int getValue()
{
return integer.intValue();
}
}

public class TestSerial
{
MyInteger i1,i2;

public TestSerial()
{
i1=new MyInteger(2);
try
{
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("tmp.obj"));
oos.writeObject(i1);
}
catch(IOException e)
{
System.out.println("Error:"+e);
}

try
{
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("tmp.obj"));
i2=(MyInteger)ois.readObject();
System.out.println("\n Value is : "+i2.getValue());
}
catch(FileNotFoundException fnfe)
{
System.out.println("Error:"+fnfe);
}
catch(IOException e)
{
System.out.println("Error:"+e);
}
catch(ClassNotFoundException cnfe)
{
System.out.println("Error:"+cnfe);
}
}
public static void main(String args[])
{
TestSerial se=new TestSerial();
}
}

OUTPUT:




















RESULT:
          Thus the Java program for the implementation of serialization was performed and the output was verified.

.
Previous
Next Post »

If you still didn't find what you're looking for, you can search this website below: