【发布时间】:2010-08-31 21:45:03
【问题描述】:
我在 2 个类中序列化一个 ArrayList:
private void serializeQuotes(){
FileOutputStream fos;
try {
fos = openFileOutput(Constants.FILENAME, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(quotesCopy);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
private void deserializeQuotes(){
try{
FileInputStream fis = openFileInput(Constants.FILENAME);
ObjectInputStream ois = new ObjectInputStream(fis);
quotesCopy = (ArrayList<Quote>) ois.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
假设:
1. Class A serializes Quotes
2. Class B deserializes Quotes
3. Class B adds stuff to Quotes
4. Class B serializes Quotes
我可以安全地假设 Quotes 将被更新并在两个类之间同步吗?
【问题讨论】:
-
(注意,您应该使用
try-finally关闭文件流。始终使用资源acquire(); try { use(); } finally { release(); }。)
标签: java serialization synchronization arraylist