hello,
i am facing problem in doing an example with partial serialization,iam unable to retrive the object even though i can store.I have 2 class parent and child in my application,where parent do not implement serialization and child implements serialization.In my child class i declare parent class object as transient and parent class has a constructor that takes arguments that i need to pass from child class.
I even used private default readObject() and writeObject() and used accordingly but facing problem to retrive object .
can any provide code snippets or example .
i will be thank ful to them
bye
this is my code snippet iam facing problem can any give solution to it
<blockquote>
code:
<pre name="code" class="core">
package org.an.ps;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class A
{
int size;
public A(int s)
{
this.size=s;
}
public int getSize()
{
return size;
}
}
class B extends A implements Serializable
{
transient A objWool=null;
public A getObjWool()
{
return objWool;
}
public B(int o1,A obj)
{
super(o1);
objWool=obj;
}
private void writeObject(ObjectOutputStream oos)
{
try {
oos.defaultWriteObject();
oos.writeInt(objWool.getSize());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void readObject(ObjectInputStream ois)
{
try {
ois.defaultReadObject();
System.out.println(ois.readInt());
objWool=new A(ois.readInt());
System.out.println(objWool.getSize());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class ParitialSerialization {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
A w1=new A(10);
B w2=new B(20,w1);
try {
FileOutputStream fos=new FileOutputStream("ps.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(w2);
oos.close();
FileInputStream fis=new FileInputStream("ps.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
//facing problem in below line //
w2=(Wool1)ois.readObject();
//System.out.println(w2);
ois.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
</pre>
</blockquote>
can any one help me to solve this problem
[ July 15, 2008: Message edited by: Joe Ess ]