Ask Question
27 September, 23:56

Assume that the reference variable r refers to a serializable object. Write code that serializes the object to the file ObjectData. dat.

+1
Answers (1)
  1. 28 September, 03:43
    0
    FileOutputStream out = new FileOutputStream ("ObjectData. dat");

    ObjectOutputStream ostream = new ObjectOutputStream (out);

    ostream. writeObject (r);

    Explanation:

    For object serialization, we can use the writeObject method of java. io. ObjectOutputStream class.

    The complete code fragment is as follows:

    import java. io.*;

    class Demo{

    public static void main (String args[]) {

    try{

    r =;

    FileOutputStream out = new FileOutputStream ("ObjectData. dat");

    ObjectOutputStream ostream = new ObjectOutputStream (out);

    ostream. writeObject (r);

    ostream. close ();

    } catch (Exception e) {

    e. printStackTrace ();

    }

    }

    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Assume that the reference variable r refers to a serializable object. Write code that serializes the object to the file ObjectData. dat. ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers