對社區的問候最近我在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