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

发表于:2007-06-22来源:作者:点击数: 标签:
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

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

原文转自:http://www.ltesting.net