• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in Serialisation

 
dinesh jangra
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the problem in this code


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Breed
{
String breed;

public Breed(String breed) {
super();
this.breed = breed;
}

public String getBreed() {
return breed;
}

public void setBreed(String breed) {
this.breed = breed;
}


}

class Animal implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
String type;

public Animal(String type) {
super();
this.type = type;
}


}

class Dog extends Animal implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 6256540233477581685L;

int size;

transient Breed breed;
public Dog(int size)
{
super("Dog");
breed=new Breed("Common");
this.size=size;
}
public Dog(int size,String Breed)
{
super("Dog");
breed=new Breed(Breed);
this.size=size;

}

public void details()
{
System.out.println(" This "+type+" is "+size+" and belongs to "+breed.getBreed());
}

private void writeObject(ObjectOutputStream oos)
{
try
{

oos.defaultWriteObject();
oos.writeObject(breed);

}catch(Exception e)
{
System.out.println("problem1");
e.printStackTrace();
}
}

private void readObject(ObjectInputStream ois)
{
try
{
ois.defaultReadObject();

breed=(Breed)ois.readObject();

}catch(Exception e)
{
System.out.println("problem2");

e.printStackTrace();
}
}


}



public class DemoSerialization1 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
FileOutputStream out=new FileOutputStream("data.str");
Dog puppy=new Dog(2);
System.out.print("Before ");
puppy.details();
ObjectOutputStream outstream=new ObjectOutputStream(out);
outstream.writeObject(puppy);
outstream.close();

puppy.size=10;

FileInputStream in=new FileInputStream("data.str");
ObjectInputStream inputStream=new ObjectInputStream(in);

puppy=(Dog) inputStream.readObject();

System.out.print("After ");
puppy.details();



}catch(Exception e)
{
e.printStackTrace();
}




}



}
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi dinesh and welcome to the JavaRanch.

Please UseCodeTags when posting code. It will make your code much easier to read.
dinesh jangra wrote:What is the problem in this code

Why don't you tell us? Please TellTheDetails. Because now we know basically nothing.
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you are trying to do with the transient command, or exactly what problem you have.

While using transient does remove Breed from being written to the file as an Object, in Dog.writeObject you specifically use writeObject to write Breed to the file as an Object. I believe that's the source of the error.

How to correct it depends on what you are trying to do. One option is to chang Breed to Serializable, or to remove transient from Breed. Or, another option would be to leave Breed without Serializable and keep transient and add a write function in Breed that is called from Dog.writeObject, and remove/replace oos.writeObject(this.breed) with this.breed.writeObject(oos).

Similar comments apply to the readObject applied to Breed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!