gpt4 book ai didi

java - 如何保存 LinkedHashMap

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:45:30 24 4
gpt4 key购买 nike

我到处搜索,尝试了不同的解决方案,但结果是空白/空/{}。是否可以保存 LinkedHashMap,以便以后检索?

//Save HashMap to Internal Storage
public void SaveHashMapToInternalStorage(String SavedData, LinkedHashMap<Integer, Integer> linkedHashMapList)
throws FileNotFoundException, IOException {
try{
File fileOne = new File(SavedData);
FileOutputStream fileOutputStream = new FileOutputStream(fileOne);
ObjectOutputStream oos = new ObjectOutputStream(fileOutputStream);

oos.writeObject(linkedHashMapList);
oos.flush();
oos.close();
fileOutputStream.close();
}catch(Exception e){}
}

//Load HashMap from Internal Storage
public LinkedHashMap<Integer, Integer> LoadHashMapFromInternalStorage(String SavedData) throws IOException {
LinkedHashMap<Integer, Integer> linkedHashMapList = new LinkedHashMap<Integer, Integer>();
try{
File toRead = new File(SavedData);
FileInputStream fileInputStream = new FileInputStream(toRead);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
linkedHashMapList = (LinkedHashMap<Integer, Integer>)objectInputStream.readObject();
objectInputStream.close();
fileInputStream.close();
for(Entry<Integer, Integer> m :linkedHashMapList.entrySet()){
linkedHashMapList.put(m.getKey(), m.getValue());
}

}catch(Exception e){}
return linkedHashMapList;
}

有人可以帮帮我吗?

非常感谢

更新:

我确实将 Toast 放在了 catch 中(异常 e)。方法有问题。

我试图在 Oncreate 方法中加载它:

    FirstRunMethod = SharedPrefs.getBoolean("FIRST_RUN", false);
if (!FirstRunMethod) {
// do the thing for the first time
prefs_editor.putBoolean("FIRST_RUN", true);
prefs_editor.apply();
.......
.......

try {
SaveHashMapToInternalStorage("file.txt", PicResolution);

} catch (FileNotFoundException e) { );
} catch (IOException e) { }
}

PictureResWidthHashMap = LoadHashMapFromInternalStorage("file.txt");

....

.....


public LinkedHashMap<Integer, Integer> LoadHashMapFromInternalStorage(String SavedData) {
LinkedHashMap<Integer, Integer> linkedHashMapLIST = new LinkedHashMap<Integer, Integer>();

try{
File toRead = new File(SavedData);
FileInputStream fileInputStream = new FileInputStream(toRead);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
linkedHashMapLIST = (LinkedHashMap<Integer, Integer>)objectInputStream.readObject();
objectInputStream.close();
fileInputStream.close();
for(Entry<Integer, Integer> m :linkedHashMapLIST.entrySet()){
linkedHashMapLIST.put(m.getKey(), m.getValue());
}

}catch(Exception e){Toast.makeText(getBaseContext(), "CANT LOAD ERROR", Toast.LENGTH_LONG).show(); }

return linkedHashMapLIST;
}

为什么它对其他人有效,但对我却无效?这对我来说毫无意义。

最佳答案

我认为这将帮助您解决问题:

试试这个:

//Save HashMap to Internal Storage
public void SaveHashMapToInternalStorage(String SavedData, LinkedHashMap<Integer, Integer> linkedHashMapList) {
try{
FileOutputStream fos = this.openFileOutput(SavedData, Context.MODE_PRIVATE);
ObjectOutputStream s = new ObjectOutputStream(fos);
s.writeObject(linkedHashMapList);
s.close();

}catch(Exception e){ }
}

//Load HashMap from Internal Storage @SuppressWarnings("unchecked")
public LinkedHashMap<Integer, Integer> LoadHashMapFromInternalStorage(String SavedData) {
LinkedHashMap<Integer, Integer> linkedHashMapLIST = new LinkedHashMap<Integer, Integer>();
try{
FileInputStream fileInputStream = this.openFileInput(SavedData);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
linkedHashMapLIST = (LinkedHashMap<Integer, Integer>) objectInputStream.readObject();
objectInputStream.close();
fileInputStream.close();

}catch(Exception e){ }
return linkedHashMapLIST;
}

关于java - 如何保存 LinkedHashMap<Integer, Integer>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28036468/

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