|
||||||
| Java Tutorials Tutorials and Code for Java |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||||
|
Java Serialization
What is Serialization? Simply put serialization is a method to deflate an object to store it on the system. So how do you do it? Simply implement the Serializable interface. Once you implement that interface, not only does your base class deflate, even you child classes deflate! What do I mean by all this, well read on. Implementing Serialization 1) Implement the interface serialzable. Code:
class Example implements Serializable. Which basically means that the object is going to be saved. 2) Time to write the object to the disk. Code:
FileOutputStream fOut = new FileOutputStream("SerializedFile.txt");
ObjectOutputStream outSt = new ObjectOutputStream(fOut);
outSt.writeObject(b1);
3) Then you decide to bring the objects back to life. So you follow the reverse process, which, naturally is called deserialization! Code:
FileInputStream fIn = new FileInputStream("SerializedFile.txt");
ObjectInputStream inSt = new ObjectInputStream(fIn);
b3 = (ObjectType)inSt.readObject();
Code:
transient int i;
The Code Code:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Base implements Serializable{
Base()
{
System.out.println("In Base Constructor");
}
public void function()
{
System.out.println("In a function");
}
}
class Derived extends Base{
Derived()
{
System.out.println("In Derived Constructor");
}
}
public class SerializeObj {
public static void main(String[] args) {
Base b1 = new Base();
Base b2 = new Base();
//---------------Writing to disk------------------------
try{
b1.function();
b2.function();
FileOutputStream fOut = new FileOutputStream("SerializedFile.txt");
ObjectOutputStream outSt = new ObjectOutputStream(fOut);
outSt.writeObject(b1);
outSt.writeObject(b2);
outSt.close();
}
catch(Exception e){
e.printStackTrace();
}
System.out.println("End of serialization");
System.out.println("Begin Deserialization");
//---------------Reading from disk----------------------
try{
FileInputStream fIn = new FileInputStream("SerializedFile.txt");
ObjectInputStream inSt = new ObjectInputStream(fIn);
Base b3 = (Base)inSt.readObject();
Base b4 = (Base)inSt.readObject();
b3.function();
b4.function();
inSt.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
Code:
In Base Constructor In Base Constructor In a function In a function End of serialization In a function In a function Ask your questions or comments here!
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
| Sponsored Links |
|
|
![]() |
| Tags |
| java, serialization, tutorial |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tutorial: Java Database Connectivity | Jordan | Java Tutorials | 0 | 05-07-2008 04:28 PM |
| Tutorial: Starting Java Using Netbeans | Jordan | Java Tutorials | 0 | 04-05-2008 02:45 PM |
| Java tutorial : my sql with java | Arkie | Java Tutorials | 2 | 04-05-2008 12:51 PM |
| John's Java Tutorial Index | John | Java Tutorials | 0 | 01-11-2007 03:05 PM |