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;
}