日期:2014-05-20  浏览次数:20669 次

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

------解决方案--------------------


//实现接口
@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);