import java.io.*; import java.lang.reflect.*; class Serializer { static void serialize(Object o, OutputStream s) throws IOException, IllegalAccessException { Class cls = o.getClass(); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) if (field.getType().equals(Integer.TYPE)) { field.setAccessible(true); s.write(field.getInt(o)); // field.getInt(o) is o.field, only in the other way around // that is, o.x and o.y in case of Points } s.flush(); } public static void main(String[] args) throws IOException, IllegalAccessException { OutputStream output = new FileOutputStream("point.data"); serialize(new Point(69, 0), output); Point p = new Point(1,2); //p.x; output.close(); } }