Java中的transient关键字介绍
transient说明一个属性是临时的,不会被序列化。
下面是一个Demo,name声明为transient,不被序列化
packagecom.zzs.tet;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.ObjectInput;
importjava.io.ObjectInputStream;
importjava.io.ObjectOutput;
importjava.io.ObjectOutputStream;
importjava.io.Serializable;
publicclassTransientDemoimplementsSerializable{
/**
*
*/
privatestaticfinallongserialVersionUID=1L;
privatetransientStringname;
privateStringpassword;
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
/**
*@paramargs
*@throwsIOException
*@throwsFileNotFoundException
*@throwsClassNotFoundException
*/
publicstaticvoidmain(String[]args)throwsFileNotFoundException,IOException,ClassNotFoundException{
//TODOAuto-generatedmethodstub
Stringpath="D:"+File.separator+"object.txt";
Filefile=newFile(path);
TransientDemotransientDemo=newTransientDemo();
transientDemo.setName("姓名");
transientDemo.setPassword("密码");
ObjectOutputoutput=newObjectOutputStream(newFileOutputStream(file));
output.writeObject(transientDemo);
ObjectInputinput=newObjectInputStream(newFileInputStream(file));
TransientDemodemo=( TransientDemo)input.readObject();
System.out.println(demo.getName()+demo.getPassword());
}
}
输出结果:
null密码
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短