Java ObjectOutputStream

The ObjectOutputStream class in Java is a subclass of the OutputStream class that provides the ability to write Java objects to an output stream. It can be used to serialize objects to a binary format that can be written to a file, sent over a network, or stored in a database.

Here are some of the commonly used methods of the ObjectOutputStream class:

  1. writeObject(Object obj): This method writes an object to the output stream. The object must be serializable, otherwise a NotSerializableException will be thrown.

  2. 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.

  3. writeUTF(String str): This method writes a string in UTF format to the output stream.

  4. write(byte[] buf): This method writes an entire byte array to the output stream.

  5. flush(): This method flushes the output stream, ensuring that any buffered data is written to the underlying output stream.

  6. 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.