Write or write an object directly
Transient keywords
Serializable interface
Externalizable interface
Example One,
Public classTest { Public Static voidMain (string[] args) {Try{T T=NewT (); T.K= 18; FileOutputStream Fos=NewFileOutputStream ("D:/bak/testobjectio.dat"); ObjectOutputStream Oos=NewObjectOutputStream (FOS); Oos.writeobject (t); Oos.flush (); Oos.close (); FileInputStream FIS=NewFileInputStream ("D:/bak/testobjectio.dat"); ObjectInputStream Ois=NewObjectInputStream (FIS); T T2=(T) ois.readobject (); System.out.println (t2.i+ "//" + T2.J + "//" + T2.D + "//" +T2.K); } Catch(Exception e) {e.printstacktrace (); } } }classTImplementsSerializable {inti = 10; intj = 9; DoubleD = 2.3; intK = 15;}
Results:
//9//2.3//
Example Two,
Modifying the variable K with transient
class Implements Serializable { int i = ten; int j = 9; double d = 2.3; transient int k =;}
Results:
//9//2.3//0
Transient modified variable k will not be written in, so the extracted k is empty, the default value of 0
Java IO (vii)-Object flow