我到处搜索,尝试了不同的解决方案,但结果为空白/空/ {}。是否可以保存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;
}

为什么它对另一个人有用,但对我却不有用?这对我完全没有意义。

最佳答案

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

试试这个:

//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;
}

10-04 19:25