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

类别:Java 点击:0 评论:0 推荐:

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

本文地址:http://com.8s8s.com/it/it17590.htm