序列化指把内存中的Java对象转换成与平台无关的二进制字节序列,以便永久保存在磁盘上或者通过网络来进行传输
将Java对象转换为字节序列的过程称为对象的序列化
如果需要让某个类支持序列化机制,该类可以通过实现Serializable接口,只需要声明实现该接口,该接口只具有标识意义
实现Serializable接口后可以通过ObjectOutputStream类对象的writeObject()方法将指定的对象序列化
public class ObjectOutputStreamTest{
public static void main(String[] args){
ObjectOutputStream oos = null;
FileOutputStream fos = null;
try{
fos = new FileOutputStream("object.dat");
oos = new ObjectOutputStream(fos);
oos.writeObject();
oos.flush();
}catch(IOException e){
e.printStackTrace();
}finally{
if(oos! = null){
try{
oos.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}