/** Read the object from Base64 string. */
private static Object fromString(String s) throws IOException, ClassNotFoundException {
byte[] data = new BASE64Decoder().decodeBuffer(s);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
Object o = ois.readObject();
ois.close();
return o;
}
/** Write the object to a Base64 string. */
private static String toString(Object o) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
oos.close();
return new String(new BASE64Encoder().encode(baos.toByteArray()));
}
java 序列化对象 反序列化对象
本文介绍如何使用Base64编码与Java的序列化机制实现对象的字符串表示及还原。通过两个核心方法,将Java对象转换为Base64编码的字符串,并能从该字符串中读取并重建原始对象。

被折叠的 条评论
为什么被折叠?



