您的位置:首页 > 编程语言 > Java开发

Java手动序列化和反序列化的实现

2013-02-26 20:12 375 查看
Java代码







//序列化对象为String字符串,先对序列化后的结果进行BASE64编码,否则不能直接进行反序列化

public static String writeObject(Object o) throws Exception {

ByteArrayOutputStream bos = new ByteArrayOutputStream();

ObjectOutputStream oos = new ObjectOutputStream(bos);

oos.writeObject(o);

oos.flush();

oos.close();

bos.close();

//return new BASE64Encoder().encode(bos.toByteArray());

return new String(bos.toByteArray(), "ISO-8859-1");

}

//反序列化String字符串为对象

public static Object readObject(String object) throws Exception{

//ByteArrayInputStream bis = new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(object));

ByteArrayInputStream bis = new ByteArrayInputStream(object.getBytes("ISO-8859-1"));

ObjectInputStream ois = new ObjectInputStream(bis);

Object o = null;

try {

o = ois.readObject();

} catch(EOFException e) {

System.err.print("read finished");

}

bis.close();

ois.close();

return o;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: