import java.io.*;
class Animal
{
String name;
Animal(){
name = "Animal";
}
public String toString(){
return name;
}
}
public class Monkey extends Animal implements Serializable
{
String name;
Monkey(){
name = "Monkey";
}
public String toString(){
return name;
}
public static void main(String... args){
ObjectOutputStream out = null;
Monkey mon = new Monkey();
System.out.println(mon);
try{
out = new ObjectOutputStream(new FileOutputStream("Monkey.ser"));
out.writeObject(mon);
}catch(IOException e){
e.printStackTrace();
}finally{
try{
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
//deserialization
ObjectInputStream in = null;
try{
in = new ObjectInputStream(new FileInputStream("Monkey.ser"));
System.out.println(in.readObject());
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(IOException io){
io.printStackTrace();
}
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7911270/viewspace-914374/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/7911270/viewspace-914374/