偶一个对象,已经实现了Serializable
现在偶想对这个对象的数组序列化,该怎么办?

解决方案 »

  1.   

    //实现接口
    @SuppressWarnings("serial")
    public class Person implements Serializable {
        private int id;
        private String name;    public Person() {
        }    public Person(int id, String name) {
            super();
            this.id = id;
            this.name = name;
        }    public int getId() {
            return id;
        }    public void setId(int id) {
            this.id = id;
        }    public String getName() {
            return name;
        }    public void setName(String name) {
            this.name = name;
        }}
    2.序列化转为数组public class SerializeUtil {
        /**
         * 序列化
         * 
         * @param object
         * @return
         */
        public static byte[] serialize(Object object) {
            ObjectOutputStream oos = null;
            ByteArrayOutputStream baos = null;
            try {
                // 序列化
                baos = new ByteArrayOutputStream();
                oos = new ObjectOutputStream(baos);
                oos.writeObject(object);
                byte[] bytes = baos.toByteArray();
                return bytes;
            } catch (Exception e) {        }
            return null;
        }