一、对象序列化(存储)
FileOutputStream fileStream = new FileOuputStream("file.ser");
ObjectOutputStream os = new ObjectOutputStream(fileStream);
os.writeObject(one);//Help one = new Help();
os.writeObject(two);
os.close();
当对象被序列化时,被该对象引用的实例变量也会被序列化,且所有被引用的对象都会被序列化。
要想类被序列化,必须实现Serializable:
public class Box implements Serializable{
private int x;
transient String y;
Animal a = new Animale();
...
}
public class Animal{
...
}
x会被保存;y不会被序列化,transient标记的变量被跳过;a不会,因为Animal类没有实现Serializable。
二、解序列化
FileInputStream fileStream