62,614
社区成员
发帖
与我相关
我的任务
分享
import java.io.*;
import java.util.*;
class Data implements Serializable { // 实现序列话接口
private int n;
public Data(int n) {
this.n = n;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public String toString() {
return Integer.toString(n);
}
}
class Worm implements Serializable {
private static final long serialVersionUID = -935278024637286504L;
public static void main(String[] args) throws ClassNotFoundException,
IOException { // 序列话读入和写入Object可能会有这两个异常
// 将你要序列化的object,保留到一个文件中
Random rand = new Random();
Data d = new Data(rand.nextInt(10)); // 构建你需要序列话的Object
System.out.println("d = " + d);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
"c:\\worm.out")); // 准备写入的文件
out.writeObject(d);
out.flush();
out.close(); // 执行到这里你可以看见worm.out这个文件,
// 以下的代码读出你刚刚写入Object
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
"c:\\worm.out")); // 读你刚刚写入的文件
Data d2 = (Data) in.readObject(); // 重新构建你刚刚写入的Object
System.out.println("d2 = " + d2);
d2.setN(99);
//然后在写入对象,不就完成修改了吗....
}
}