[java]代码库
import java.io.*;
public class SerializableDemo {
public static void main(String args[]) throws IOException,
ClassNotFoundException {
Student stu = new Student(1564163, "xxx", 18, "CSD");
FileOutputStream fo = new FileOutputStream("data.ser");
// 保存对象的状态
ObjectOutputStream so = new ObjectOutputStream(fo);
try {
so.writeObject(stu);
so.close();
} catch (IOException e) {
System.out.println(e);
}
FileInputStream fi = new FileInputStream("data.ser");
ObjectInputStream si = new ObjectInputStream(fi);
// 恢复对象的状态
try {
stu = (Student) si.readObject();
si.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
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;
}
}
初级程序员
by: 正仔 发表于:2013-04-02 14:52:14 顶(1) | 踩(0) 回复
这个挺实用的O(∩_∩)O~~
回复评论