import java.io.Serializable;
public class Student implements Serializable{
String id, name;
int age;
public Student(String id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
import java.io.*;
import java.util.Arrays;
public class Main2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Student stu1 = new Student("001","aa",22);
Student stu2 = new Student("002","bb",221);
Student stu3 = new Student("003","cc",2321);
Student stu4 = new Student("004","dd",234123);
Student[] students = new Student[]{stu1,stu2,stu3,stu4};
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("stu.dat"));
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("stu.dat"));
oos.writeObject(students);
Object object = ois.readObject();
if(object instanceof Student[]){
Student[] stus = (Student[])object;
for(Student stu: stus){
System.out.println(stu);
}
}
oos.close();
ois.close();
}
}