看到网上很多博客都是写到transient修饰的变量不可以被序列化,
实际上其变量可以被序列化,《Java编程思想》上写的很清楚。
事实证明:看博客不如看书(看经典书籍),看书不如自己码代码验证
代码是借鉴别人的,为了验证问题,直接上代码:
import java.io.File;
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 PasswordFormat implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private transient String password;
public PasswordFormat(String name, String password) {
this.name = name;
this.password = password;
}
public PasswordFormat() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private void writeObject(ObjectOutputStream oos) throws IOException {//重写以下两个方法,完成序列化时password的加密和解密
oos.defaultWriteObject();
char a[]=this.password.toCharArray();
String str="";
for(int i=0;i<a.length;i++){
a[i]+=i;
str+=a[i];
}//各项加i再逆置;
StringBuilder sb = new StringBuilder(str);
oos.writeObject(sb.reverse().toString());
}
private void readObject(ObjectInputStream ois)
throws ClassNotFoundException, IOException {
ois.defaultReadObject();
String str = (String) ois.readObject();
System.out.println("解密后:" + str);
StringBuilder sb = new StringBuilder(str);
sb.reverse();
char a[]=sb.toString().toCharArray();
str="";
for(int i=0;i<a.length;i++){
a[i]-=i;
str+=a[i];
}
this.password = str;
}
@Override
public String toString() {
return "PasswordFormat [name=" + name + ", password=" + password + "]";
}
public static void serializeTemp(PasswordFormat ob,File file){
try(
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
){
oos.writeObject(ob);
oos.flush();
System.out.println((PasswordFormat)ois.readObject());
}catch(IOException | ClassNotFoundException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
PasswordFormat pf=new PasswordFormat("xcw","xcw12345678");
File file=new File("./yj.txt");
PasswordFormat.serializeTemp(pf,file);
}
}
image
上图可以看到,只要重写writeObject和readObject,
- 非transinent修饰的变量使用defaultWriteObject()和defaultReadObject()进行序列化和反序列化
- transient修饰的变量必须手动使用writeObject()和readObject()进行序列化和反序列化