2

所以我对序列化场景很陌生,我不知道是否可以序列化哈希表然后将其保存到文件中......但这是我迄今为止尝试过的......出于某种原因到我的代码部分而不是执行 Try 块?

public void addDataIntoFlashCardFile(Context context, Hashtable<Integer, ArrayList<Deck>> data) {
        try {
            FileOutputStream fos = context.openFileOutput(
                    FLASHCARDS_FILENAME, Context.MODE_PRIVATE
                            | Context.MODE_APPEND);
            ObjectOutputStream osw = new ObjectOutputStream(fos);
            osw.writeObject(data);

    } catch (FileNotFoundException e) {
        // catch errors opening file
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, "calles", Toast.LENGTH_SHORT).show();
    }
    }

在这里,我尝试从文件中读取它(这不起作用,因为它首先不会写入文件)

try {

            Hashtable<Integer, ArrayList<Deck>> temp = new Hashtable<Integer, ArrayList<Deck>>();
            FileInputStream myIn = context.openFileInput(FLASHCARDS_FILENAME);
            ObjectInputStream IS = new ObjectInputStream(myIn);
            Toast.makeText(context, "here", Toast.LENGTH_SHORT).show();
            try {

                temp = (Hashtable<Integer, ArrayList<Deck>>)IS.readObject();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            }
            IS.close();

            //testing purposes
            for (int i = 0; i < temp.size(); i++) {
                for (int p = 0; p < temp.get(i).size(); p++) {
                    for (int q = 0; q < temp.get(i).get(p).getDeck().size(); q++) {
                    Toast.makeText(context, temp.get(i).get(p).getDeck().get(q).getQuestion(), Toast.LENGTH_LONG).show();
                    }
                }
            }
        }
        catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(context, "here", Toast.LENGTH_SHORT).show();
        }
    }
4

1 回答 1

1

您实际上可以使用 JSONObject。

第 1 步:创建您的 JSONObject:

JSONObject myAwesomeObject = new JSONObject();
JSONArray myAwesomeArray = new JSONArray();

第 2 步:遍历 HashMap 并将其添加到 JSONObject

for (Entry<Integer, ArrayList<Deck>> entry : map.entrySet())
{
    ArrayList<Deck> decks = temp.get(entry);
    JSONObject JSONDeck = new JSONObject();
    for (Deck deck : decks){
        JSONDeck.add("deck", deck.getWhateverDataDeckContains());
    }
    myAwesomeArray.add("deck", JSONDeck);
}
myAwesomeObject.add("deck_collection", myAwesomeArray);

第 3 步:获取包含您的表的字符串:

String myAwesomeContents = myAwesomeObject.toString();

第 4 步:将其作为纯文本插入文件中。

要反序列化它,您只需遍历您的 JSONObject 并填写一个新表。

第 1 步:从文件中取回字符串。

第 2 步:创建包含该字符串的 JSONObject 的新实例:

JSONObject deserialized = new JSONObject(stringContainingYourDataInJSON);

第 3 步:取回您的简单数据:

ArrayList<Deck> decksArray = new ArrayList<Deck>();
JSONArray decks = deserialized.getJSONArray("deck_collection");
for (int i=0; i<decks.length(); i++){
    JSONObject deck = decks.get(i);
    String name = deck.getString("name");
    int length = deck.getInt("length");
    // etc
    Deck deckPojo = new Deck();
    deckPojo.setWhatever(whateverParamsYouWantToSet);
    decksArray.add(deckPojo);
 }
于 2012-11-07T08:47:51.807 回答