关于 transient的总结

来源:互联网 发布:windows调试模式 编辑:程序博客网 时间:2024/09/15 10:44

最经的项目中有用到序列化和反序列化的东西,可以对于有些字段的反序列化总是得不到想要的值,折磨了好久,后来一个同事提醒了我,是关于transient的问题,所以特别写了个小例子来描述这个关键字在序列化中的应用

 

package com.eric.transienttest;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TransientTest {
 public static void main(String[] args) {
  Person p1 = new Person("Eric", "123456");
  Person2 p2 = new Person2("Simon", "123456");
  
  readAndWriteObject(p1);
  readAndWriteObject(p2);  

//由于people2的pwd字段被设置了transient 所以其打印的结果如下

//before write file:person1 name:Eric pwd:123456
//get object from file:person1 name:Eric pwd:123456
//before write file:person2 name:Simon pwd:123456
//get object from file:person2 name:Simon pwd:null
  
 }
 
 static void readAndWriteObject(People people) {
  
  System.out.println("before write file:" + people);
  try {
   ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("people.out"));
   o.writeObject(people);
   o.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  
  // To read the object back, we can write
  
  try {
   ObjectInputStream in = new ObjectInputStream(new FileInputStream("people.out"));
   People people2 = (People) in.readObject();
   System.out.println("get object from file:" + people2);
  } catch (Exception e) {// deal with exception}
  
  }
 }
}

class People {
 
}

class Person extends People implements Serializable {
 /**
     *
     */
 private static final long serialVersionUID = -9221500999229526064L;
 private String           name;
 private String           pwd;
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 public String getPwd() {
  return pwd;
 }
 
 public void setPwd(String pwd) {
  this.pwd = pwd;
 }
 
 public Person(String name, String pwd) {
  super();
  this.name = name;
  this.pwd = pwd;
 }
 
 @Override
 public String toString() {
  return "person1 name:" + name + " pwd:" + pwd;
 }
 
}

class Person2 extends People implements Serializable {
 /**
     *
     */
 private static final long serialVersionUID = -3195698226754948666L;
 private String           name;
 private transient String pwd;                                      // 这个不会被序列化和反序列化
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 public String getPwd() {
  return pwd;
 }
 
 public void setPwd(String pwd) {
  this.pwd = pwd;
 }
 
 public Person2(String name, String pwd) {
  super();
  this.name = name;
  this.pwd = pwd;
 }
 
 @Override
 public String toString() {
  return "person2 name:" + name + " pwd:" + pwd;
 }
}