This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes paritial serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "paritial serialization" Watch "paritial serialization" New topic
Author

paritial serialization

anish reddy
Ranch Hand

Joined: Apr 19, 2008
Posts: 74
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 ]
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
but facing problem to retrive object


Are you getting any error messages? Please copy and post them.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

There are a whole bunch of little issues here, and I don't know for sure which one you're running into. First, your "main" won't compile, as you're not catching ClassNotFoundException, which readObject() can throw. Second, you can't deserialize your parent class because it has no no-argument constructor, although you won't find that out until you try and get an exception. Third, in one place you mention a class "Wool1" -- I think you just forgot to change that to "B" when you posted the code. It's better to post real code so that sort of thing doesn't happen; in fact I suspect all three of these are just translation issues!

Then the final issue, and perhaps the one you're really having, is shown in these two lines:

System.out.println(ois.readInt());
objWool=new A(ois.readInt());


You write only one extra int to the file, but here, you're reading two. You'll get an EOFException on the second line above.


[Jess in Action][AskingGoodQuestions]
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

anish:
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.


Why do you need to have an instance of the parent class in the child class?
You either choose inheritance or delegation, you are doing both!

This post in the sun forum discuss in detail what you want to do:
[ July 16, 2008: Message edited by: Nitesh Kant ]

apigee, a better way to API!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: paritial serialization
 
Similar Threads
calling servlet from java program and more
De-serialization
Plz help me............
Problem with read-write methods in Serialization
Behaviour of static variables