Two Laptop Bag*
The moose likes Beginning Java and the fly likes Serialization Handing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Serialization Handing

Ma JianHui
Greenhorn

Joined: Mar 01, 2008
Posts: 6
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
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Please use code tags for posting the code.

Seems your query got skipped.Please post your query.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
. . . and welcome to the Ranch. I have already replied on your other thread; suggest you ask a moderator to close this thread.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
Please don't post the same question multiple times.


Ping & DNS - my free Android networking tools app
 
wood burning stoves
 
subject: Serialization Handing