gpt4 book ai didi

java - 如何从文件中读取和重写单例对象?

转载 作者:行者123 更新时间:2023-12-01 06:57:23 26 4
gpt4 key购买 nike

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 的内容是持久的。

编辑:因为我说得不够清楚。

我应该在哪里放置检查和读取此类的先前实例所需的代码?我猜是在构造函数中,但是我的列表也会获取另一个对象的值吗?我不知道在哪里/如何编码。

编辑2:粘贴解决方案。

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");
}

}

感谢您的帮助! :)

最佳答案

您可以将以下方法添加到存储对象:

public void persist() throws IOException{

FileOutputStream fos = null;
ObjectOutputStream out = null;
try{
fos = new FileOutputStream(FILE_NAME); //assumes filename is a constant you've defined
out = new ObjectOutputStream(fos);
out.writeObject(time);
}finally{
out.close();
}
}



protected static void initialize() throws IOException{
FileInputStream fis = null;
ObjectInputStream in = null;
try{
fis = new FileInputStream(FILE_NAME);
in = new ObjectInputStream(fis);
instance = (PersistentTime)in.readObject();
}finally{
in.close();
}
}

您可以从静态实例方法中调用initialize(),而不是直接调用构造函数。

关于java - 如何从文件中读取和重写单例对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8361301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com