1.序列化保存
public class ObjectOutPutStream_ {
public static void main(String[] args) throws Exception {
//序列化后保存的文件格式,不是纯文本,而是按照它的格式来保存
String filePath = "e:\\data.dat";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
//序列化数据到e:\data.dat
oos.writeInt(100);//int-->Integer(实现了Serializable)
oos.writeBoolean(true);//boolean-->Boolean(实现了Serializable)
oos.writeChar('a');//char-->Character(实现了Serializable)
oos.writeDouble(99.9);//double-->Double(实现了Serializable)
oos.writeUTF("本草纲目");//String(实现了Serializable)
oos.writeObject(new Dog("小黄",3));
oos.close();
}
}
//如果需要保存某个类的对象,该类必须实现序列化接口
class Dog implements Serializable {
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;