public static void seria(){
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("person")));
Person person = new Person();
person.setAge(18);
person.setName("huah");
oos.writeObject(person);
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void enSeria(){
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("person")));
Person person =(Person) ois.readObject();
System.out.println(person);
} catch (Exception e) {
e.printStackTrace();
}
}
//序列化
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
//反序列化
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
Object o = ois.readObject();
java序列化&反序列化
最新推荐文章于 2024-07-15 21:55:17 发布