Serialization involves saving the current state of an object to a stream, and restoring an equivalent object from that stream.
For an object to be serialized, it must be an instance of a class that implements either the Serializable or Externalizable interface. Both interfaces only permit the saving of data associated with an object's variables.
They depend on the class definition being available to the Java Virtual Machine at reconstruction time in order to construct the object.
The Serializable interface relies on the Java runtime default mechanism to save an object's state.
Writing an object is done via the writeObject() method in the ObjectOutputStream class (or the ObjectOutput interface).
Writing a primitive value may be done through the appropriate write<datatype>() method.
Reading the serialized object is accomplished using the readObject() method of the ObjectInputStream class, and primitives may be read using the various read<datatype>() methods.
The Externalizable interface specifies that the implementing class will handle the serialization on its own, instead of relying on the default runtime mechanism.
This includes which fields get written (and read), and in what order.
The class must define a writeExternal() method to write out the stream, and a corresponding readExternal() method to read the stream.
Inside of these methods the class calls ObjectOutputStream writeObject(), ObjectInputStream readObject(), and any necessary write<datatype>() and read<datatype>() methods, for the desired fields.
class DataOrder implements Serializable{
String apples, peaches, pears, cardnum, custID;
double icost;
int itotal;
}
Saving the current state of an object to a stream
DataOrder orders = new DataOrder();
FileOutputStream fos =
new FileOutputStream(orders);
ObjectOutputStream oos =
new ObjectOutputStream(fos);
oos.writeObject(order);
restoring an equivalent object from that stream
FileInputStream fis =
new FileInputStream(orders);
ObjectInputStream ois =
new ObjectInputStream(fis);
order = (DataOrder)ois.readObject();
| Answered By : |
null Replied Date : Jan 29 2012 |
| Answer : |
|
| Answered By : |
null Replied Date : Dec 9 2011 |
| Answer : |
|
| Answered By : |
null Replied Date : Dec 6 2011 |
| Answer : |
|
| Answered By : |
null Replied Date : Oct 29 2011 |
| Answer : |
|
| Answered By : |
null Replied Date : Jul 6 2011 |
| Answer : |
|
| Answered By : |
null Replied Date : Jun 22 2011 |
| Answer : |
|
| Answered By : |
null Replied Date : Jun 17 2011 |
| Answer : |
|
| Answered By : |
null Replied Date : May 29 2011 |
| Answer : |
|
| Answered By : |
null Replied Date : Apr 18 2011 |
| Answer : |
|
| Answered By : |
null Replied Date : Apr 17 2011 |
| Answer : |
|
| Answered By : |
null Replied Date : Apr 5 2011 |
| Answer : |
|
| Answered By : |
null Replied Date : Mar 19 2011 |
| Answer : |
|
| Answered By : |
null Replied Date : Feb 9 2011 |
| Answer : |
|
| Answered By : |
null Replied Date : Feb 6 2011 |
| Answer : |
|
| Answered By : |
null Replied Date : Jan 15 2011 |
| Answer : |
|
| Answered By : |
null Replied Date : Jan 15 2011 |
| Answer : |
|
| Answered By : |
null Replied Date : Jan 6 2011 |
| Answer : |
|
| Answered By : |
null Replied Date : Dec 31 2010 |
| Answer : |
|
| Answered By : |
null Replied Date : Dec 29 2010 |
| Answer : |
|
| Answered By : |
null Replied Date : Dec 23 2010 |
| Answer : |
|
| Answered By : |
null Replied Date : Dec 22 2010 |
| Answer : |
|
| Answered By : |
null Replied Date : Nov 30 2010 |
| Answer : |
|
| Answered By : |
null Replied Date : Nov 21 2010 |
| Answer : |
|
| Answered By : |
null Replied Date : Nov 16 2010 |
| Answer : |
|
| Answered By : |
null Replied Date : Oct 18 2010 |
| Answer : |
|
| Answered By : |
null Replied Date : Oct 17 2010 |
| Answer : |
|
| Answered By : |
null Replied Date : Oct 15 2010 |
| Answer : |
|
| Answered By : |
null Replied Date : Oct 9 2010 |
| Answer : |
|
| Answered By : |
null Replied Date : Aug 22 2010 |
| Answer : |
|
| Answered By : |
null Replied Date : Aug 21 2010 |
| Answer : |
|
| Answered By : |
null Replied Date : Jun 12 2012 |
| Answer : |
|
| Answered By : |
null Replied Date : Jun 25 2012 |
| Answer : |
|
| Answered By : |
null Replied Date : Sep 15 2012 |
| Answer : |
|
| Answered By : |
null Replied Date : Oct 10 2012 |
| Answer : |
|
| Answered By : |
null Replied Date : Nov 29 2012 |
| Answer : |
|
| Answered By : |
null Replied Date : Jan 2 2013 |
| Answer : |
|
| Answered By : |
null Replied Date : Feb 23 2013 |
| Answer : |
|
| Answered By : |
null Replied Date : Apr 18 2013 |
| Answer : |
|