public class Student implements Serializable{
private int id;
private String name;
private String password;
public Student() {
}
public Student(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
}
public class ReadObject {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Student stu=new Student(4,"ls","123456");
File file=new File("D:/stuobj.txt");
if(!file.exists()){
file.createNewFile();
}
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(stu);
oos.close();
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
Student tempstu=(Student)ois.readObject();
System.out.println(tempstu.getId()+"::"+tempstu.getName()+"::"+tempstu.getPassword());
}
}