193k views
3 votes
Assume that the reference variable r refers to a serializable object. Write code that serializes the object to the file ObjectData.dat.

User Firegnom
by
5.5k points

1 Answer

0 votes

Answer:

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

ObjectOutputStream ostream = new ObjectOutputStream(out);

ostream.writeObject(r);

Step-by-step 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 = <Reference to Object to be serialized> ;

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

ObjectOutputStream ostream = new ObjectOutputStream(out);

ostream.writeObject(r);

ostream.close();

} catch(Exception e){

e.printStackTrace();

}

}

}

User Etheros
by
6.0k points