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

誰かが私を助けてくれますか?

どうもありがとう

アップデート:

トーストを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 に答える