Ask Question
28 July, 22:06

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

+2
Answers (1)
  1. 28 July, 22:38
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers