用java实现一个简单的序列化的例子
2008-01-05 09:38:38 来源:WEB开发网package test;
import java.io.*;
public class Test implements Serializable{
int i=0;
//不让变量j序列化
transient int j=0;
public static void main(String[] args) {
Test test=new Test();
test.i=3;
test.j=7;
System.out.PRintln(test.i);
System.out.println(test.j);
//存
FileOutputStream fileStream = null;
try {
fileStream = new FileOutputStream("c:\test.obj");
ObjectOutputStream out = new ObjectOutputStream(fileStream);
out.writeObject(test);
out.close();
}
catch (Exception ex) {
}
try {
//取
FileInputStream fileStream1 = new FileInputStream("c:\prodUCt.obj");
ObjectInputStream in = new ObjectInputStream(fileStream1);
Test test1 = (Test)in.readObject();
System.out.println(test1.i);
//j没有被序列化
System.out.println(test1.j);
in.close();
}
catch (Exception ex) {
}
}
}
更多精彩
赞助商链接