Serializing/Deserializing objects is very useful if you want to use a Java object in the future. The following code shows how to serialize an object.

 Object object = new String("Hello World");
    
try {

    // Serialize to a file
    ObjectOutput out = new ObjectOutputStream(new FileOutputStream("filename.ser"));
    out.writeObject(object);
    out.close();

} catch (IOException e) {
    e.printStackTrace();
}