服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

用java实现一个简单的序列化的例子

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) {
}
}
}

扫描关注微信公众号