自定义序列化 减少序列化对象的大小
定义一个实现序列化接口的bean 并重写readObject和writeObject方法
实现类中所有的字段都使用transient 修饰、表示在序列化的时候不保存该字段
package mytest;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.lang.reflect.Field;import java.lang.reflect.Modifier;import java.util.ArrayList;import java.util.Date;import java.util.List;public class TestBean implements Serializable {/** * */private static final long serialVersionUID = 1L;private transient Long code;private transient String text;private transient Date d;private transient List<String> list;private transient List<String> list2;public Long getCode() {return code;}public void setCode(Long code) {this.code = code;}public String getText() {return text;}public void setText(String text) {this.text = text;}public Date getD() {return d;}public void setD(Date d) {this.d = d;}public List<String> getList() {return list;}public void setList(List<String> list) {this.list = list;}public List<String> getList2() {return list2;}public void setList2(List<String> list2) {this.list2 = list2;}/** * 序列化对象的方法的writeObject * @param os * @throws IOException * @throws IllegalAccessException * @throws IllegalArgumentException */private void writeObject(ObjectOutputStream os) throws IOException, IllegalArgumentException,IllegalAccessException {os.defaultWriteObject();//java对象序列化默认操作Field[] fs = this.getClass().getDeclaredFields();for (Field field : fs) {String f = field.getName();if ("serialVersionUID".equals(f))continue;int mod = field.getModifiers();if (Modifier.isTransient(mod))setObjectOutputStream(os, field);}}/** * 反序列化对象的方法readObject * @param is * @throws IOException * @throws ClassNotFoundException * @throws IllegalArgumentException * @throws IllegalAccessException */private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException, IllegalArgumentException,IllegalAccessException {is.defaultReadObject();//java对象反序列化默认操作Field[] fs = this.getClass().getDeclaredFields();for (Field field : fs) {String f = field.getName();if ("serialVersionUID".equals(f))continue;int mod = field.getModifiers();if (Modifier.isTransient(mod))readObjectInputStream(is, field);}}@SuppressWarnings("unchecked")private void setObjectOutputStream(ObjectOutputStream os, Field field) throws IOException,IllegalArgumentException, IllegalAccessException {Class<?> type = field.getType();Object v = field.get(this);if (type == String.class || type == Long.class || type == Integer.class) {os.writeObject(v);} else if (type == java.util.Date.class || type == java.sql.Date.class) {os.writeObject(v);} else if (type == int.class) {os.writeInt(Integer.parseInt(v.toString()));} else if (type == long.class) {os.writeLong(Long.parseLong(v.toString()));} else if (type == List.class) {List<String> l = (List<String>) v;os.writeInt(l.size());for (String s : l) {os.writeObject(s);}}}private void readObjectInputStream(ObjectInputStream is, Field f) throws IllegalArgumentException,IllegalAccessException, IOException, ClassNotFoundException {Class<?> type = f.getType();if (type == String.class || type == Long.class || type == Integer.class) {f.set(this, is.readObject());} else if (type == java.util.Date.class || type == java.sql.Date.class) {f.set(this, is.readObject());} else if (type == int.class) {f.set(this, is.readInt());} else if (type == long.class) {f.set(this, is.readLong());} else if (type == List.class) {int size = is.readInt();List<String> setList = new ArrayList<String>();for (int i = 0; i < size; i++) {Object data = is.readObject();setList.add(data.toString());}f.set(this, setList);}}}
package mytest;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.Date;public class SerializeTest {public static void main(String[] args) throws IOException, ClassNotFoundException {TestBean tb = new TestBean();//tb.setCode(null);tb.setText("中文");tb.setD(new Date());List<String> list = new ArrayList<String>();list.add("1");list.add("2");list.add("3");list.add("4");tb.setList(list);List<String> list2 = new ArrayList<String>();list2.add("1");list2.add("2");list2.add("3");list2.add("4");list2.add("5");tb.setList2(list2);ByteArrayOutputStream bos = new ByteArrayOutputStream();ObjectOutputStream out = new ObjectOutputStream(bos);out.writeObject(tb);out.flush();out.close();System.out.println(new String(bos.toByteArray()));System.out.println(bos.size());ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));TestBean tb2 = (TestBean) in.readObject();System.out.println(tb2.getD());}}