ObjectOutputStream 

Introduction

The Java.io.ObjectOutputStream class writes primitive data types and graphs of Java objects to an OutputStream.The objects can be read (reconstituted) using an ObjectInputStream.

Class declaration

Following is the declaration for Java.io.ObjectOutputStream class −

public class ObjectOutputStream
   extends OutputStream
      implements ObjectOutput, ObjectStreamConstants

Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

Class constructors

Sr.No.Constructor & Description
1protected ObjectOutputStream()This provides a way for subclasses that are completely reimplementing ObjectOutputStream to not have to allocate private data just used by this implementation of ObjectOutputStream.
2ObjectOutputStream(OutputStream out)This creates an ObjectOutputStream that writes to the specified OutputStream.

Class methods

Sr.No.Method & Description
1protected void annotateClass(Class <?> cl)Subclasses may implement this method to allow class data to be stored in the stream.
2protected void annotateProxyClass(Class<?> cl)Subclasses may implement this method to store custom data in the stream along with descriptors for dynamic proxy classes.
3void close()This method closes the stream.
4void defaultWriteObject()This method writes the non-static and non-transient fields of the current class to this stream.
5protected void drain()This method drain any buffered data in ObjectOutputStream.
6protected boolean enableReplaceObject(boolean enable)This method enable the stream to do replacement of objects in the stream.
7void flush()This method flushes the stream.
8ObjectOutputStream.PutField putFields()This method retrieves the object used to buffer persistent fields to be written to the stream.
9protected Object replaceObject(Object obj)This method will allow trusted subclasses of ObjectOutputStream to substitute one object for another during serialization.
10void reset()This method reset will disregard the state of any objects already written to the stream.
11void useProtocolVersion(int version)This method specify stream protocol version to use when writing the stream.
12void write(byte[] buf)This method writes an array of bytes.
13void write(byte[] buf, int off, int len)This method writes a sub array of bytes.
14void write(int val)This method writes a byte.
15void writeBoolean(boolean val)This method writes a boolean.
16void writeByte(int val)This method writes an 8 bit byte.
17void writeBytes(String str)This method writes a String as a sequence of bytes.
18void writeChar(int val)This method writes a 16 bit char.
19void writeChars(String str)This method writes a String as a sequence of chars.
20protected void writeClassDescriptor(ObjectStreamClass desc)This method writes the specified class descriptor to the ObjectOutputStream.
21void writeDouble(double val)This method writes a 64 bit double.
22void writeFields()This method writes the buffered fields to the stream.
23void writeFloat(float val)This method writes a 32 bit float.
24void writeInt(int val)This method writes a 32 bit int.
25void writeLong(long val)This method writes a 64 bit long.
26void writeObject(Object obj)This method writes the specified object to the ObjectOutputStream.
27protected void writeObjectOverride(Object obj)This method is used by subclasses to override the default writeObject method.
28void writeShort(int val)This method writes a 16 bit short.
29protected void writeStreamHeader()This method is provided so subclasses can append or prepend their own header to the stream.
30void writeUnshared(Object obj)This method writes an “unshared” object to the ObjectOutputStream.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.Object

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *