import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
使用transient关键字不序列化某个变量
注意读取的时候,读取数据的顺序一定要和存放数据的顺序保持一致
*/
public class TransientTest {
static String PATH = “F:/user.txt”;
public static void main(String[] args) {
User user = new User();
user.setUsername(“Alexia”);
user.setPasswd(“123456”);
System.out.println("read before Serializable: ");
System.out.println("username: " + user.getUsername());
System.out.println("password: " + user.getPasswd());
try {
ObjectOutputStream os = new ObjectOutputStream(
new FileOutputStream(PATH));
os.writeObject(user); // 将User对象写进文件
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
ObjectInputStream is =
new ObjectInputStream(new FileInputStream(PATH));
user = (User) is.readObject(); // 从流中读取User的数据
is.close();
System.out.println("\nread after Serializable: ");
System.out.println("username: " + user.getUsername());
System.out.println("password: " + user.getPasswd());
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
}
class User implements Serializable {
private static final long serialVersionUID = 8294180014912103005L;
private String username;
private transient String passwd;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
}
输出结果:
密码字段为null,说明反序列化时根本没有从文件中获取到信息。
2. transient使用小结
1)被transient关键字修饰的变量不能被序列化,变量将不再是对象持久化的一部分,该变量内容在序列化后无法通过反序列化获得。
2)transient关键字只能修饰变量,而不能修饰方法和类。注意,本地变量是不能被transient关键字修饰的。变量如果是用户自定义类变量,则该类需要实现Serializable接口。
3)一个静态变量不管是否被transient修饰,均不能被序列化。
第三点可能有些人很迷惑,因为发现在User类中的username字段前加上static关键字后,程序运行结果依然不变,即static类型的username也读出来为“Alexia”了,这不与第三点说的矛盾吗?实际上是这样的:第三点确实没错(一个静态变量不管是否被transient修饰,均不能被序列化),反序列化后类中static型变量username的值为当前JVM中对应static变量的值,这个值是JVM中的不是反序列化得出的,不相信?好吧,下面我来证明:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class TransientTest {
static String PATH = “F:/user.txt”;
public static void main(String[] args) {
User user = new User();
user.setUsername(“Alexia”);
user.setPasswd(“123456”);
System.out.println("read before Serializable: ");
System.out.println("username: " + user.getUsername());
System.out.println("password: " + user.getPasswd());
try {
ObjectOutputStream os = new ObjectOutputStream(
new FileOutputStream(PATH));
os.writeObject(user); // 将User对象写进文件
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
// 在反序列化之前改变username的值
User.username = “jmwang”;
ObjectInputStream is =
new ObjectInputStream(new FileInputStream(PATH));
user = (User) is.readObject(); // 从流中读取User的数据
is.close();
System.out.println("\nread after Serializable: ");
System.out.println("username: " + user.getUsername());
System.out.println("password: " + user.getPasswd());
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
}