import java.io.*;
class MyClass implements Serializable {
private static final long serialVersionUID = 1L;
int id;
String name;
public MyClass(int id, String name) {
this.id = id;
this.name = name;
}
}
public class SerializationExample {
public static void main(String[] args) {
MyClass obj = new MyClass(1, "Example");
try {
FileOutputStream fileOut = new FileOutputStream("object.ser");
ObjectOutputStream outStream = new ObjectOutputStream(fileOut);
outStream.writeObject(obj);
outStream.close();
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
MyClass deserializedObj = null;
try {
FileInputStream fileIn = new FileInputStream("object.ser");
ObjectInputStream inStream = new ObjectInputStream(fileIn);
deserializedObj = (MyClass) inStream.readObject();
inStream.close();
fileIn.close();
e.printStackTrace();
return;
}
}
}