对社区的问候最近我在java项目中提出了序列化和反序列化问题。我有一个对象的类包含其他对象作为字段。如何序列化一个java对象内部不包含Serializable字段为字节数组并反序列化数组以获取原始对象
我想对象的状态存储到一个字节数组然后反序列化的字节数组,并返回原来的对象不过,由我对象的字段不可序列化对象(来自第三党的图书馆)所以不得不首先宣布它们是暂时的。
现在我的对象是序列化和反序列化的,但是由于我提到过的暂态声明它的字段为空,我尝试在本地创建Serialization类中的所有元素,并为它们分配原始的价值观并继续这一过程,但它没有任何区别。我引用我的代码的一部分,任何想法?由于事先:)
这里是类我的对象与它的领域
public class AbePublicKey implements java.io.Serializable{
private static final long serialVersionUID = 7526472295622776147L;
public transient Element g;
public transient Element h;
public transient Element f;
public transient Element e_g_g_hat_alpha;
}
这里是我的串行功能
public byte[] PublicKeytoByteArray(AbePublicKey publickey) throws IOException {
KeyAuthority keyauthority = new KeyAuthority();
byte[] bytes = null;
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
publickey.setElements(g, h, f, e_g_g_hat_alpha);
try {
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(publickey);
oos.flush();
bytes = bos.toByteArray();
} finally {
if (oos != null)
oos.close();
}
if (bos != null) {
bos.close();
}
}
return bytes;
}
这里是我的解串器功能
public static AbePublicKey PublicKeyBytestoObject(byte[] publickeybytes) throws IOException, ClassNotFoundException {
AbePublicKey obj = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
try {
bis = new ByteArrayInputStream(publickeybytes);
ois = new ObjectInputStream(bis);
obj = (AbePublicKey) ois.readObject();
} finally {
if (bis != null) {
bis.close();
}
if (ois != null) {
ois.close();
}
}
return obj;
}
可能重复的[Java Custom Serialization](http://stackoverflow.com/questions/7290777/java-custom-serialization) – sidgate