In this tutorial, we will learn about Java ObjectOutputStream and its methods with the help of examples.
An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream. Only objects that support the java.io.Serializable interface can be written to streams. The class of each serializable object is encoded including the class name and signature of the class, the values of the object’s fields and arrays, and the closure of any other objects referenced from the initial objects. The Java ObjectOutputStream is often used together with a Java ObjectInputStream. The ObjectOutputStream is used to write the Java objects, and the ObjectInputStream is used to read the objects again.
public class ObjectOutputStream
extends OutputStream
implements ObjectOutput, ObjectStreamConstants
Sr.No. | Method & Description |
---|---|
1 | protected void annotateClass(Class <?> cl)
Subclasses may implement this method to allow class data to be stored in the stream. |
2 | protected void annotateProxyClass(Class<?> cl)
Subclasses may implement this method to store custom data in the stream along with descriptors for dynamic proxy classes. |
3 | void close()
This method closes the stream. |
4 | void defaultWriteObject()
This method writes the non-static and non-transient fields of the current class to this stream. |
5 | protected void drain()
This method drain any buffered data in ObjectOutputStream. |
6 | protected boolean enableReplaceObject(boolean enable)
This method enable the stream to do replacement of objects in the stream. |
7 | void flush()
This method flushes the stream. |
8 | ObjectOutputStream.PutField putFields()
This method retrieves the object used to buffer persistent fields to be written to the stream. |
9 | protected Object replaceObject(Object obj)
This method will allow trusted subclasses of ObjectOutputStream to substitute one object for another during serialization. |
10 | void reset()
This method reset will disregard the state of any objects already written to the stream. |
11 | void useProtocolVersion(int version)
This method specify stream protocol version to use when writing the stream. |
12 | >void write(byte[] buf)
This method writes an array of bytes. |
13 | void write(byte[] buf, int off, int len)
This method writes a sub array of bytes. |
14 | void write(int val)
This method writes a byte. |
15 | void writeBoolean(boolean val)
This method writes a boolean. |
16 | void writeByte(int val)
This method writes an 8 bit byte. |
17 | >void writeBytes(String str)
This method writes a String as a sequence of bytes. |
18 | void writeChar(int val)
This method writes a 16 bit char. |
19 | void writeChars(String str)
This method writes a String as a sequence of chars. |
20 | protected void writeClassDescriptor(ObjectStreamClass desc)
This method writes the specified class descriptor to the ObjectOutputStream. |
21 | void writeDouble(double val)
This method writes a 64 bit double. |
22 | void writeFields()
This method writes the buffered fields to the stream. |
23 | void writeFloat(float val)
This method writes a 32 bit float. |
24 | void writeInt(int val)
This method writes a 32 bit int. |
25 | void writeLong(long val)
This method writes a 64 bit long. |
26 | void writeObject(Object obj)
This method writes the specified object to the ObjectOutputStream. |
27 | protected void writeObjectOverride(Object obj)
This method is used by subclasses to override the default writeObject method. |
28 | void writeShort(int val)
This method writes a 16 bit short. |
29 | protected void writeStreamHeader()
This method is provided so subclasses can append or prepend their own header to the stream. |
30 | void writeUnshared(Object obj)
This method writes an "unshared" object to the ObjectOutputStream. |