The
Transient indicates that a property is temporary and will not be serialized.
The following is a demo,name declared as transient and not serialized
Package com.zzs.tet;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.ObjectInput;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutput;
Import Java.io.ObjectOutputStream;
Import java.io.Serializable;
public class Transientdemo implements serializable{/** * */private static final long serialversionuid = 1L;
private transient String name;
private String password;
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;
/** * @param args * @throws ioexception * @throws filenotfoundexception * @throws classnotfoundexception */public static void main (string[] args) throws FileNotFoundException, IOException, classnotfoundexception {//TODO A Uto-generateD method Stub String path= "D:" +file.separator+ "Object.txt";
File File=new file (path);
Transientdemo transientdemo=new Transientdemo ();
Transientdemo.setname ("name");
Transientdemo.setpassword ("password");
ObjectOutput output=new ObjectOutputStream (new FileOutputStream (file));
Output.writeobject (Transientdemo);
ObjectInput input=new ObjectInputStream (new FileInputStream (file));
Transientdemo demo= (Transientdemo) input.readobject ();
System.out.println (Demo.getname () +demo.getpassword ()); }
}
Output results:
Null password