我到处搜索,我尝试了不同的解决方案,但结果是空白/空/{}。是否可以保存 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(Exception 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;
}
为什么它对其他人有效,但对我无效?这对我来说毫无意义。