Java - ObjectOutputStream reset() method



Description

The Java ObjectOutputStream reset() method will disregard the state of any objects already written to the stream. The state is reset to be the same as a new ObjectOutputStream. The current point in the stream is marked as reset so the corresponding ObjectInputStream will be reset at the same point. Objects previously written to the stream will not be referred to as already being in the stream. They will be written to the stream again.

  • reset() clears the internal cache of previously written objects.

  • This is useful when

  • You want to re-serialize the same object with updated state.

  • You want to avoid shared references from earlier writes.

By default, Java's serialization keeps track of objects already written, and reuses references instead of writing them again. reset() breaks that reference sharing.

Declaration

Following is the declaration for java.io.ObjectOutputStream.reset() method.

public void reset()

Parameters

NA

Return Value

This method does not return a value.

Exception

IOException − If reset() is invoked while serializing an object.

Example - Usage of ObjectOutputStream reset() method

The following example shows the usage of ObjectOutputStream reset() method.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      Object s = "Hello World!";
      Object s2 = "Bye World!";
      
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeObject(s);

         // reset the stream and rewrite what is already written
         oout.reset();

         // write something again
         oout.writeObject(s2);

         // close the stream
         oout.close();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read and print a string
         System.out.println("" + (String) ois.readObject());
         System.out.println("" + (String) ois.readObject());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Hello World!
Bye World!

Example - Rewriting a modified object without reset() (does not work)

The following example shows the usage of ObjectOutputStream reset() method.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("reset1.ser"))) {
         Data d = new Data("first");

         oos.writeObject(d); // writes "first"
         d.value = "modified"; // modify the object
         oos.writeObject(d); // Java skips re-writing full object, just writes a reference

         System.out.println("Two writes without reset().");

         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("reset1.ser"));
         Data d1 = (Data)ois.readObject();
         System.out.println("Data = " + d1.value);
      }catch(ClassNotFoundException ce){
         ce.printStackTrace();
      }
   }

   static class Data implements Serializable {
      String value;
      public Data(String value) {
         this.value = value;
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Two writes without reset().
Data = first

Explanation

  • Only the first value ("first") is saved fully.

  • The second write only stores a reference - so "modified" doesn't make it into the stream.

Example - Rewriting a modified object with reset() (works as expected)

The following example shows the usage of ObjectOutputStream reset() method.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) throws IOException, ClassNotFoundException {
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("reset2.ser"))) {
         Data d = new Data("first");

         oos.writeObject(d);         // writes "first"
         oos.reset();                // clears object cache

         d.value = "modified";       
         oos.writeObject(d);         // writes "modified" as fresh object

         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("reset2.ser"));
         Data d1 = (Data)ois.readObject();
         Data d2 = (Data)ois.readObject();

         System.out.println("Two writes with reset(). Updated value = " + d2.value);
      }
   }

   static class Data implements Serializable {
      String value;
      public Data(String value) {
         this.value = value;
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Two writes with reset(). Updated value = modified
java_io_objectoutputstream.htm
Advertisements