Java tutorial
//package com.java2s; //License from project: Apache License import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.io.OutputStream; public class Main { public static void serialize(Object object, File file) { try { serialize(object, new FileOutputStream(file)); } catch (final FileNotFoundException e) { throw new IllegalArgumentException("File must exist.", e); } } public static void serialize(Object object, OutputStream outputStream) { try { final OutputStream buffer = new BufferedOutputStream(outputStream); final ObjectOutput output = new ObjectOutputStream(buffer); try { output.writeObject(object); } finally { output.close(); } } catch (final IOException e) { throw new RuntimeException(e); } } }