Serialize an object to an output stream in Java
Description
The following code shows how to serialize an object to an output stream.
Example
// www . java 2 s. c om
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class Main {
public static void serializeObject(File file, Object object) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
try {
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(fos));
oos.writeObject(object);
oos.flush();
} finally {
fos.close();
}
}
}