如何序列化对象数组? 偶一个对象,已经实现了Serializable现在偶想对这个对象的数组序列化,该怎么办? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //实现接口@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; } 火急!!!如何用js给tabled的属性width赋值? 基于udp的socket通信的数据接收问题? 字节数组[]转换成图象文件,文件保存为空 一个ArrayList里的元素全部是Byte对象.怎么才能把这个ArrayList转换成一个byte[](不是Byte[]) AWT中的按钮 Java如果定时向网页提交数据的程序? jbuilder8编辑的问题 int/string 初学者向大家请教 flc +java? Java界面设计 Object... elements
@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;
}