ObjectOutputstream对象输出流
public class ObjectOutputStream01{
public static void main(String[] args) throws Exception{
String filePath = "D:\\data.dat";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
oos.writeInt(100); //int -> Integer(实现了Serializable)
oos.writeBoolean(true); //boolean -> Boolean
oos.writeChar('a'); //char -> Character
oos.writeDouble(9.5); //double -> Double
oos.writeUTF("周杰伦"); //String
oos.writeObject(new Dog("旺财",10));
oos.close();
System.out.println("数据保存完毕(序列化形式)");
}
}
class Dog implements Serializable {
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}