[java] view plaincopyprint?import java.io.*;
public class Serialization {
public static void main(String [] args) {
Student stu1 = new Student(1, “Ronnie”, 37, “snooker”);
Student stu2 = new Student(2, “John”, 37, “snooker”);
try {
ObjectOutputStream s = new ObjectOutputStream(new FileOutputStream(new File(“student.txt”)));
os.writeObject(stu1);
os.writeObject(stu2);
os.close();
ObjectInputStream is = new ObjectInputStream(new FileInputStream(new File(“student.txt”)));
System.out.println((Student)is.readObject());
System.out.println((Student)is.readObject());
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Student implements Serializable {
int id;
String name;
int age;
String department;
public Student(int id, String name, int age, String department) {
this.id = id;
this.name = name;
this.age = age;
this.department = department;
}
public String toString() {
return “name:” + name + “, id:” + id + “, age:” + age + “, department:” + department;
}
}
更多IT资料请访问:Tore_m_628846_6316_5_1.html”>http://www.shangxueba.com/sTore_m_628846_6316_5_1.html
把你的脸迎向阳光,那就不会有阴影