public class Storage implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public static List<Message> MessageList = Collections.synchronizedList(new ArrayList<Message>()); //Fail safe if multiple threads modify them.
public static List<Group> GroupList = Collections.synchronizedList(new ArrayList<Group>());
protected Storage() {
super();
}
static private Storage _instance = null;
//initialized: Storage.instance();
static public Storage instance() {
if(_instance == null) {
_instance = new Storage();
}
return _instance;
}
}
我有创建单个对象的上层类。我想将这个对象的列表保存到一个文件中。然后当我的应用程序启动时,我实例化存储我希望它读取文件,如果它是空创建一个新的存储,但如果它不然后读取以前的存储实例,并创建基于旧的这个新的。基本上意味着我希望GroupList和MessageList的内容持久。如何从文件中读取和重写单例对象?
编辑:因为我没有说清楚。
我在哪里放置检查和读取此类的前一个实例所需的代码?我想在构造函数中,但是然后我的列表也会得到另一个对象的值?我不知道在哪里/如何编码。
EDIT2:粘贴解决方案。
public class Storage implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public static List<Message> MessageList = Collections.synchronizedList(new ArrayList<Message>()); //Fail safe if multiple threads modify them.
public static List<Group> GroupList = Collections.synchronizedList(new ArrayList<Group>());
protected Storage() {
super();
}
static private Storage _instance = null;
//initialized: Storage.instance();
public static synchronized Storage instance(){
initialize();
if(_instance == null) {
_instance = new Storage();
}
return _instance;
}
public static synchronized void persist(){
FileOutputStream fos = null;
ObjectOutputStream out = null;
try{
fos = new FileOutputStream("Storage.txt");
out = new ObjectOutputStream(fos);
out.writeObject(instance());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
protected static synchronized void initialize(){
FileInputStream fis = null;
ObjectInputStream in = null;
try{
fis = new FileInputStream("Storage.txt");
in = new ObjectInputStream(fis);
_instance = (Storage)in.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (OptionalDataException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static synchronized void addElement(Message message){
if(!MessageList.contains(message)){
MessageList.add(message);
persist();
Log.i("STORAGE-addElement", "Added: " + message);
}
}
public static synchronized void addElement(Group group){
if(!GroupList.contains(group)){
GroupList.add(group);
persist();
Log.i("STORAGE-addElement", "Added: " + group);
}
}
public static synchronized void removeElement(Message message){
if(!MessageList.contains(message)){
MessageList.remove(message);
persist();
Log.i("STORAGE-removeElement", "Removed: " + message);
}
}
public static synchronized void removeElement(Group group){
if(!GroupList.contains(group)){
GroupList.remove(group);
persist();
Log.i("STORAGE-removeElement", "Removed: " + group);
}
}
public static synchronized void wipeAll(){
MessageList.clear();
GroupList.clear();
persist();
Log.i("STORAGE-wipeAll", "Wiped all data");
}
}
感谢您的帮助! :)
当我不得不做这种事情时,我使用了dom4j库,它允许您非常容易地将对象保存到xml文件。现在可能有更好的办法 –
这个过程的哪一部分是你遇到的问题? – Shaun
@Shaun编辑。 – Miky