import java.io.*;
class MyClass implements Serializable{
String a;
int b;
double c;
MyClass(String a, int b, double c){
this.a = a;
this.b = b;
this.c = c;
}
public String toString(){
return "a="+a+";b="+b+";c="+c;
}
};
public class test {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
try{
MyClass object1 = new MyClass("Hello", -7, 2.7);
FileOutputStream fos = new FileOutputStream("serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object1);
oos.close();
}catch(Exception e){
System.out.println("err");
}
try{
MyClass object2;
FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
object2 = (MyClass)ois.readObject();
ois.close();
System.out.println("Object2:"+object2);
}catch(Exception e){
System.out.println("err");
}
}
}
|