@Test
public void writeObj(){
UserInterface user = new User();
//user.say("hello");
try {
FileOutputStream fos = new FileOutputStream("user.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(user);
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void readObj(){
try {
FileInputStream fis = new FileInputStream("user.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
UserInterface user = (UserInterface) ois.readObject();
user.say("hello");
//打印值为 E/---: User{name='jack', age=20, gender='man'}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
常用的序列化与反序列化
最新推荐文章于 2024-07-25 13:40:37 发布