我有序列化爲一個文件對象的列表:Java的反序列化如何忽略屬性
class MyClass implements Serializable {
public String location;
public SomeClass lastUpdatedAt;
}
不幸的是,我現在有這個類的一個新版本,我們沒有SomeClass的了;原因是複雜的,但我需要序列化這回這個新的形式(這是一個例子類,而不是實際的類明顯):
class MyClass implements Serializable {
public String location;
}
總之,我想忽略在反序列化的屬性。
請不要改變原來的類源文件。我只有在新的源文件
然而ObjectInputStream的當它試圖找到SomeClass的在classpath失敗:
try {
FileInputStream fis;
ObjectInputStream is;
File f = getSavedFile();
if (f.exists()) {
fis = new FileInputStream(f);
is = new ObjectInputStream(fis);
ArrayList<MyClass> result = (ArrayList<MyClass>) is.readObject(); //fails
is.close();
fis.close();
}
}
catch (Exception e) {
//ClassNotFound exception because SomeClass is not on the classpath.
e.printStackTrace();
}
我知道你可以擴展ObjectInputStream的和實施的東西在那裏,但是我對序列化並不熟悉,希望有人解決同樣的問題。
與上述相同的問題。我無法對原始來源進行任何更改。它是一個正在運行的服務,我需要更新一個全新的,同時也保留遺留數據序列化到文件....總之 – breakline