小编Jes*_*erN的帖子

如何将 HashMap 保存到 Android 中的文件?

我正在尝试将用户设置保存到一个文件中,我可以从那里读取稍后的内容。但我无法让它正常工作。我试过阅读这个,但我仍然有问题。

Map<String, String> userSettings = new HashMap<>();

public void updateUserSettings(){

        userSettings.clear();

        userSettings.put("item0", item0);
        userSettings.put("item1", item1);
        userSettings.put("item2", item2);
        userSettings.put("item3", item3);
        userSettings.put("item4", item4);
        userSettings.put("item5", item5);
        userSettings.put("item6", item6);
        userSettings.put("item7", item7);


        userSettings.put("i0", Float.toString(i0));
        userSettings.put("i1", Float.toString(i1));
        userSettings.put("i2", Float.toString(i2));
        userSettings.put("i3", Float.toString(i3));
        userSettings.put("i4", Float.toString(i4));
        userSettings.put("i5", Float.toString(i5));
        userSettings.put("i6", Float.toString(i6));
        userSettings.put("i7", Float.toString(i7));

        userSettings.put("huvudMaskin", huvudMaskin);
        userSettings.put("minorMaskin1", minorMaskin1);
        userSettings.put("minorMaskin2", minorMaskin2);

        userSettings.put("maskinTid", Float.toString(maskinTid));
        writeSettings();
    }



public void writeSettings() {
    try
    {
        FileOutputStream fos = context.openFileOutput("test.ser", Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(userSettings);
        oos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}


public …
Run Code Online (Sandbox Code Playgroud)

java android dictionary hashmap save

5
推荐指数
2
解决办法
2672
查看次数

标签 统计

android ×1

dictionary ×1

hashmap ×1

java ×1

save ×1