• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

paritial serialization

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Master Rancher
Posts: 5112
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but facing problem to retrive object



Are you getting any error messages? Please copy and post them.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic