• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Doubts in Serialization

 
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;
}

]
 
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.
 
Could you hold this kitten for a sec? I need to adjust this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!