Java tutorial
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; public class Main { public static <T> byte[] serializeToByteArray(T object) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(object); } catch (Exception e) { e.printStackTrace(); return null; } return baos.toByteArray(); } }