1个回答
推荐于2016-09-26
展开全部
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Student implements Serializable {
private static final long serialVersionUID = -6601903208557464574L;
private String id;
private String name;
public Student() {
super();
}
public Student(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class SerializeToFileTest {
public static void main(String[] args) throws IOException,
ClassNotFoundException {
Student o = new Student("1001", "张三");
FileOutputStream os = new FileOutputStream("D:\\student.data");
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(o);
oos.flush();
oos.close();
FileInputStream is = new FileInputStream("D:\\student.data");
ObjectInputStream ois = new ObjectInputStream(is);
Student deserialized = (Student) ois.readObject();
ois.close();
System.out.println("学号:" + deserialized.getId());
System.out.println("姓名:" + deserialized.getName());
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询