Package com. io. test;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. ObjectInputStream;
Import java. io. ObjectOutputStream;
Import java. io. Serializable;
Import org. junit. Test;
/**
* Directly write or read an object
*
* @ Author chengjj
*
*/
Public class TestObjectStream {
@ Test
Public void testObjectStream (){
Try {
T t = new T ();
T. k = 8;
FileOutputStream fos = new FileOutputStream ("E:/4.txt ");
ObjectOutputStream oos = new ObjectOutputStream (fos );
Oos. writeObject (t );
Oos. flush ();
Oos. close ();
FileInputStream FCM = new FileInputStream ("E:/4.txt ");
ObjectInputStream ois = new ObjectInputStream (FCM );
T otherT = (T) ois. readObject ();
System. out. println (otherT. I );
System. out. println (otherT. h );
System. out. println (otherT. d );
System. out. println (otherT. k );
Ois. close ();
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
}
}
}
/**
* Mark the interface and mark it to the compiler.
* Member variables modified by transient are not considered during serialization.
* @ Author chengjj
*
*/
Class T implements Serializable {
Int I = 10, h = 9;
Double d = 2.3;
Transient int k = 15;
}