Java Methods of ObjectOutputStream
The ObjectOutputStream
class in Java provides a number of methods for writing serialized objects to an output stream. Some of the commonly used methods of the ObjectOutputStream
class are:
writeObject(Object obj)
: This method writes an object to the output stream. The object must be serializable, otherwise aNotSerializableException
will be thrown.writeInt(int val)
,writeBoolean(boolean val)
,writeByte(byte val)
,writeChar(char val)
,writeFloat(float val)
,writeDouble(double val)
,writeLong(long val)
,writeShort(short val)
: These methods write the corresponding primitive data type to the output stream.writeUTF(String str)
: This method writes a string in UTF format to the output stream.write(byte[] buf)
: This method writes an entire byte array to the output stream.defaultWriteObject()
: This method writes the default serialization for an object. It is typically used when the custom writeObject method needs to write some or all of the data to the stream.writeObjectOverride(Object obj)
: This method is called to write the specified object to the stream. It can be overridden by subclasses to provide a custom implementation.useProtocolVersion(int version)
: This method sets the serialization protocol version to be used by the output stream.writeUnshared(Object obj)
: This method writes an object to the stream, but ensures that it is not shared with any other object previously written to the stream.reset()
: This method clears the internal state of the output stream, which is useful if you want to reuse the stream to write a new set of objects.close()
: This method closes the output stream and releases any system resources associated with it.
It's important to note that the ObjectOutputStream
class should be used with caution, especially when writing data that will be read by an ObjectInputStream
from an untrusted source, as it can be vulnerable to malicious attacks if the data is not properly validated.