Return to Snippet

Revision: 19750
at October 29, 2009 23:52 by arunpjohny


Initial Code
package serialize;

import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

public class IOUtils {
	public static void serialize(OutputStream out, Serializable obj) {
		try {
			ObjectOutputStream stream = new ObjectOutputStream(out);
			stream.writeObject(obj);
		} catch (IOException e) {
			throw new IOError(e);
		}
	}

	public static <T> T deserialize(InputStream in, Class<? extends T> c) {
		try {
			ObjectInputStream stream = new ObjectInputStream(in);
			T obj = c.cast(stream.readObject());
			return obj;
		} catch (IOException e) {
			throw new IOError(e);
		} catch (ClassNotFoundException e) {
			throw new IOError(e);
		}
	}
}

Initial URL


Initial Description
The given utility class can be used to serialize and deserialize java objects

Initial Title
Serialize and Deserialize Objects

Initial Tags


Initial Language
Java