这篇文章主要介绍怎么用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:est.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) {
}
}
}
以上是“怎么用java实现序列化”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网行业资讯频道!