3

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

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

4

2 回答 2

2

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

尝试这个:

//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;
}
于 2015-01-21T00:25:24.460 回答
1

可能你在初始化时犯了一些错误。我已经运行了这段代码并得到了所需的输出。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedHashMap;
import java.util.Map.Entry;


public class LinkedHashMapExample {

    public LinkedHashMapExample() {
        // TODO Auto-generated constructor stub
    }

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

    /**
     * @param args
     */
    public static void main(String[] args) throws FileNotFoundException, IOException
    {
        // TODO Auto-generated method stub

        LinkedHashMap<Integer, Integer> linkedHashMap = new LinkedHashMap<Integer,Integer>();

        linkedHashMap.put(0, 0);
        linkedHashMap.put(1, 1);
        linkedHashMap.put(2, 4);

        LinkedHashMapExample linkedHashMapExample = new LinkedHashMapExample();

        linkedHashMapExample.SaveHashMapToInternalStorage("file.txt", linkedHashMap);

        LinkedHashMap<Integer, Integer> linkedHashMapOutput = linkedHashMapExample.LoadHashMapFromInternalStorage("file.txt");

        for(int i=0; i <linkedHashMapOutput.size(); i++)
        {
            System.out.println(linkedHashMapOutput.get(i));
        }
    }

}

在输出中我得到:

0 1 4

希望你能从这个例子中找出你的问题。

于 2015-01-20T06:03:00.403 回答