Download Sample Code - WordPress.com

skillfulwolverineΛογισμικό & κατασκευή λογ/κού

2 Δεκ 2013 (πριν από 3 χρόνια και 6 μήνες)

69 εμφανίσεις

package chap6.serialization;


import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;


/**


* @author Vivek


*
Serializable is a marker interface; it has no methods to implement.

*/


public class TestSerialization implements Serializable {


private static final long serialVersionUID = 7549941751033992353L;




String path = "D:/test/serialized.ser";


/**



*

Worki
ng with ObjectOutputStream and ObjectInputStream



*

The magic of basic serialization happens with just two methods: one to serialize objects



*

and write them to a stream, and a second to read the stream and deserialize objects.



*

ObjectOutputStream
.writeObject() // serialize and write



* ObjectInputStream.readObject() // read and deserialize



*/




public static void main(String[] args) {



TestSerialization testSerialization = new TestSerialization();

//


testSerialization.serialize();

//


testSerialization.deserialize();



testSerialization.testTransient();


}



private void testTransient() {



ReferenceClass c1 = new ReferenceClass();



ReferenceClass c2 = new ReferenceClass("ooo");

//


ReferenceClass2 c2 = new ReferenceClass2();



ReferenceClass2 c3 = new ReferenceClass2(123, c1);






/******************Serialize c2***********************/

//


try{

//



ObjectOutputStream objectOutputStream = new ObjectOutputStream(new
FileOutputStream(path));


//



objectOutputStream.writeObject(c
2);

//



objectOutputStream.flush();

//



objectOutputStream.close();

//




//



ObjectInputStream objInputStream = new ObjectInputStream(new
FileInputStream(path));

//



objInputStream.readObject();

//


}catch (Exception e) {

//



e.printStackTrace();

//


}






/******************Serialize c3***********************/



try{




System.out.println(c3.clazz.className);




ObjectOutputStream objectOutputStream = new ObjectOutputStream(new
FileOutputStream(path));





objectOutputStream.writeObject(c3);




objectOutputStream.flush();




objectOutputStream.close();








ObjectInputStream objInputStream = new ObjectInputStream(new
FileInputStream(path));




objInputStream.readObject();




System.out.println("AFTER:

"+c3.clazz.className);




System.out.printl
n("AFTER:

"+c3.clazz.testTransient_1);




System.out.println("AFTER:

"+c3.clazz.testTransient_2);




System.out.println("AFTER:

"+c3.clazz.testTransient_3);







}catch (Exception e) {




e.printStackTrace();



}


}



private void deserialize() {



try{




FileInputStream fis = new FileInputStream(path);




ObjectInputStream ois = new ObjectInputStream(fis);




TestSerialization readObject = (TestSerialization)ois.readObject();




ois.close();







}catch (Exception e) {




e.printStackTrace();



}


}



private void serialize(){






TestSerialization testSerialization = new TestSerialization();




try{





FileOutputStream fos = new FileOutputStream(path);





ObjectOutputStream oos = new ObjectOutputStream(fos);





oos.writeObject(testSerialization);





oos.close();









}catch (Exception e) {





e.PrintStackTrace();







}


}

}

class ReferenceClass{


String className="hi";


String testTransient_1;


int testTransient_2;


Integer testTransient_3;


public ReferenceClass() {





}




public Reference
Class(String className) {



this.className = className;


}



}


class ReferenceClass2 implements Serializable{




private static final long serialVersionUID = 8247251094152377594L;


int hashCode;


/**



* Object references marked transient will always be reset to null,



* regardless of whether they were initialized at the time of declaration in



* the class.



*/


public transient ReferenceClass clazz;




public ReferenceClass2() {





}




public Refe
renceClass2(int hashCode,ReferenceClass referenceClass) {



this.hashCode = hashCode;



this.clazz = referenceClass;


}




/***************************************************/


/**



* These methods steps in middle of serialization & deserialization.



*
These methods allows you to update values of transient variables.



*/


private void writeObject(ObjectOutputStream oos){



try {




System.out.println("Update Value of transient var in ReferenceClass using
writeObject");




oos.defaultWriteObject();




clazz.className = "test";




oos.writeChars(clazz.className);



} catch (IOException e) {




e.printStackTrace();



}


}




private void readObject(ObjectInputStream ois){



try {




System.out.println("reading Updated Value of transient var in ReferenceCl
ass
using readObject");




ois.defaultReadObject();




clazz = new ReferenceClass();




clazz.className = ois.readLine();



} catch (IOException e) {




e.printStackTrace();



} catch (ClassNotFoundException e) {




e.printStackTrace();



}


}


/**********
******************************************/