gpt4 book ai didi

java - 序列化后在多个类中使用相同的映射?

转载 作者:太空宇宙 更新时间:2023-11-04 12:13:49 25 4
gpt4 key购买 nike

我的应用程序在停止之前保存 HashMap ,并在再次启动时加载相同的 HashMap ,以便可以对其进行更改。我正在使用序列化。

存储类别:

public class Storage {

private Map<String, String> storage;
private String projectStorageFilePath;

public Storage() {
this.storage = new ConcurrentHashMap<String, String>();
makeDir();
}

/**
* If the file in which the map objects will be saved doesn't exist in the
* user home directory it creates it.
*/
private void makeDir() {
File projectHomeDir = new File(System.getProperty("user.home"), ".TestMap");
String projectHomeDirPath = projectHomeDir.getAbsolutePath();
File projectStorageFile = new File(projectHomeDirPath, "storage.save");
projectStorageFilePath = projectStorageFile.getAbsolutePath();
if (!projectHomeDir.exists()) {
projectHomeDir.mkdir();
try {
projectStorageFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}

@SuppressWarnings("unchecked")
public boolean load() {
boolean isLoaded = false;
ObjectInputStream ois = null;
try {

File file = new File(projectStorageFilePath);
if (file.length() != 0) {

//loading the map
ois = new ObjectInputStream(new FileInputStream(file));
storage = (ConcurrentHashMap<String, String>) ois.readObject();

isLoaded = true;

}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (null != ois) {
ois.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return isLoaded;
}

public boolean save() {
boolean isSaved = false;
ObjectOutputStream oos = null;
try {

//saving
oos = new ObjectOutputStream(new FileOutputStream(projectStorageFilePath));
oos.writeObject(storage);

isSaved = true;

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != oos) {
oos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return isSaved;
}

public Map<String, String> getStorage() {
return this.storage;
}
}

我尝试使用该 HashMap 执行某些操作的类:

public class DoSomethingWithMap {

private Map<String, String> storage;

public DoSomethingWithMap(Map<String, String> storage) {
this.storage = storage;
}

public void addToMap(String key, String value) {
this.storage.put(key, value);
}
public void printMap() {
System.out.println(this.storage);
}
}

当我第一次运行它时,它工作正常:

public class Main {

public static void main(String[] args) {
Storage s = new Storage();
DoSomethingWithMap something = new DoSomethingWithMap(s.getStorage());

if (s.load()) {
System.out.println(s.getStorage());
}

something.addToMap("2", "test2");
something.addToMap("4", "test4");
something.addToMap("5", "test5");

if (s.save()) {
System.out.println(s.getStorage());
}
}
}

输出:

{}           //empty map which is ok because it has never been saved before
{3=test3, 4=test4, 5=test5} //changes during runtime are saved

问题是当我再次启动 Main 并尝试更改已保存的 map 时:

public static void main(String[] args) {
Storage s = new Storage();
DoSomethingWithMap something = new DoSomethingWithMap(s.getStorage());

if (s.load()) {
System.out.println(s.getStorage());
}

something.printMap();

something.addToMap("6", "newTest");
something.addToMap("7", "newTest");
something.addToMap("8", "newTest");

something.printMap();

if (s.save()) {
System.out.println(s.getStorage());
}
}

输出:

{3=test3, 4=test4, 5=test5}         //loading the map works fine
{} //here it should be same as previous line but is not
{6=newTest, 7=newTest, 8=newTest} //DoSomethingWithMap.printMap is printing only the changes during runtime
{3=test3, 4=test4, 5=test5} // changes during runtime are not saved

很明显 DoSomethingWithMap 类没有使用提供给它的 map 。为什么?使用哪张 map ?我该如何解决这个问题?

谢谢。

最佳答案

您正在加载方法中创建 Map 的新实例:

storage = (ConcurrentHashMap<String, String>) ois.readObject();

要解决此问题,您可以清除当前 map ,然后添加加载的 map 中的所有值:

//loading the map
ois = new ObjectInputStream(new FileInputStream(file));
storage.clear();
storage.putAll((ConcurrentHashMap<String, String>) ois.readObject());

为了防止将来出现此类错误,您可以将这些字段设为最终字段,这样您就会收到错误报告。

关于java - 序列化后在多个类中使用相同的映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39615537/

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