Ask Question

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

+5
Answers (1)
  1. 28 December, 07:03
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers