当需要把一个对象保存在文件、数据库中,或是将其在网络上传播的时候,就需要对其进行序列化。序列化和反序列化就是分别把对象转成字节序列和把字节序列恢复成对象的过程。实现Serializable接口是应用序列化与反序列化的一种方式。这个接口没有方法,这种叫标记接口,表示实现这个接口的类可以被序列化。若类中的某个属性不想被序列化时,可以加上transient关键字修饰,这样在序列化时,这个属性就会按其类型的默认值被序列化(如int的默认值为0)。
Serializable接口是由JDK来控制对象的序列化,也可以使用Externalizable接口,这样就能自主控制对象的序列化过程,需要重写readExternal和writeExternal方法。
使用序列化与反序列化时还要用到对象输入输出流(ObjectOutputStream和ObjectInputStream),使用这两个流的时候需要与文件输入输出流进行套接。
以下是示例代码,运行后控制台会输出Car{brand='benz', color='null', price=100},可以看到color属性并不是black,而是字符串的默认值,因为Car类中color属性被transient修饰。另外,该实例中使用了不少流,这些流都需要关闭,关闭时应遵循先开启的后关闭的原则。并且关闭前应先判断其是否为空,关闭一个未开启的流会抛出异常。
public class SerialiableTest {
public static void main(String[] args) {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fos = new FileOutputStream("D:/serializable.ser");
oos = new ObjectOutputStream(fos);
Car benz = new Car("benz", "black", new BigDecimal(100));
oos.writeObject(benz);
oos.flush();
oos.close();
fis = new FileInputStream("D:/serializable.ser");
ois = new ObjectInputStream(fis);
Car res = (Car) ois.readObject();
System.out.println(res);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (ois != null) {
ois.close();
}
if (fis != null) {
fis.close();
}
if (oos != null) {
oos.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class Car implements Serializable {
private String brand;
private transient String color;
private BigDecimal price;
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", color='" + color + '\'' +
", price=" + price +
'}';
}
public Car(String brand, String color, BigDecimal price) {
this.brand = brand;
this.color = color;
this.price = price;
}
}