• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubts in Serialization

 
shashi booshan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys in the following im gettin problem with Dog () and the Collar().Not gettin it properly.can somebody help me ?




import java.io.*
public class SerializeDog {
public static void main(String[] args)
{
Collar c = new Collar(3);
Dog d = new Dog(c,7);
System.out.println("Before serialization the collar size is :"+ getCollar() + d.getCollarSize());

try {
FileOutputStream fos = new FileOutputStream("myfile.txt");
ObjectOutputStream os = new ObjectOutputStream (fos);
os.writeObject(d);

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

try {
FileInputStream fis = new FileInputStream("myfile.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
d = (Dog) ois.readObject();
ois.close();
}
catch(Exception e) {
e.printStackTrace();
}
System.out.println("Before serialization the collar size is :"+ d.getCollar + d.getCollarSize());
}
}
Class Dog implements Serializable {
private Collar theCollar;
private int dogSize;
public Dog(Collar collar,int size) {
theCollar = collar;
dogSize = size;
}

public Collar getCollar() {
return theCollar;
}

Class Collar implements Serializable {
private int collarSize;
public Collar(int size) {
collarSize = size;
}
public getCollarSize();
return collarSize;
}

]
 
kamal shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<blockquote>code:
<pre name="code" class="core"> import java.io.*;
public class SerializeDog {
public static void main(String[] args){
Collar c = new Collar(3);
Dog d = new Dog(c,7);
System.out.println("Before serialization the collar size is :"+ (d.getCollar()).getCollarSize());
System.out.println("Collor referece is ::"+d.getCollar());
System.out.println("Dog referece is ::"+d);
try {
FileOutputStream fos = new FileOutputStream("myfile.txt");
ObjectOutputStream os = new ObjectOutputStream (fos);
os.writeObject(d);

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

try {
FileInputStream fis = new FileInputStream("myfile.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
d = (Dog) ois.readObject();
ois.close();
}catch(Exception e) {
e.printStackTrace();
}

System.out.println("After deserialization the collar size is :"+ (d.getCollar()).getCollarSize());
System.out.println("Collor referece is ::"+d.getCollar());
System.out.println("Dog referece is ::"+d);
}
}

class Dog implements Serializable {
private Collar theCollar;
private int dogSize;

public Dog(Collar collar,int size) {
theCollar = collar;
dogSize = size;
}
public Collar getCollar() {
return theCollar;
}
}

class Collar implements Serializable {
private int collarSize;

public Collar(int size) {
collarSize = size;
}

public int getCollarSize(){
return collarSize;
}
}
</pre>
</blockquote>

Shashi Run the above code,And watch the dog and collar references values.It will surely clear your doubt.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic