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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Serialization Handing

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
import java.io.*;

public class TestSerilization {

public static void main(String[] args) {
Cat c = new Cat(3);
Dog d = new Dog(c,4);
p("before: "+d.getCat().getI());

FileOutputStream fs = null;

try {
fs = new FileOutputStream("D:/HanJia/seria.ser");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(d);
os.close();
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
FileInputStream fis= null;
try {
fis = new FileInputStream("D:/HanJia/seria.ser");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(fis);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
d = (Dog)ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

p("after: "+d.getCat().getI());

}

static void p(Object o) {
System.out.println(o);
}
}


import java.io.Serializable;

public class Dog implements Serializable{
transient private Cat c;
int i;
public Dog(Cat c,int i) {
this.c = c;
this.i = i;
}

public Cat getCat() {
return c;
}
}


import java.io.*;

public class Cat implements Serializable{
private int a;
public static int c = 3;

public float b = 5.9f;

public Cat(int a) {
this.a = a;
}

int getI() {
return a;
}
}

I declar
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please use code tags for posting the code.

Seems your query got skipped.Please post your query.
 
Marshal
Posts: 79966
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
. . . and welcome to the Ranch. I have already replied on your other thread; suggest you ask a moderator to close this thread.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please don't post the same question multiple times.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    Bookmark Topic Watch Topic
  • New Topic