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

Jes*_*erN 5 java android dictionary hashmap save

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

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 void readSetttings() {
    try
    {
        FileInputStream fileInputStream = new FileInputStream(context.getFilesDir()+"test.ser");
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        Map myHashMap = (Map)objectInputStream.readObject();
        userSettings = null;
        userSettings = myHashMap;
    }
    catch(ClassNotFoundException | IOException | ClassCastException e) {
        e.printStackTrace();
    }
    executeSettings();
}
Run Code Online (Sandbox Code Playgroud)

我拥有该应用程序的读取和写入权限。

我没有从中得到任何东西。我检查了哈希图,它按预期工作。我也尝试了很多不同的方法,我唯一能做的就是将字符串保存到 .txt 文件中。

Pra*_*een 8

 private String subFolder = "/userdata";
private String file = "test.ser";

public void writeSettings() {
    File cacheDir = null;
    File appDirectory = null;

    if (android.os.Environment.getExternalStorageState().
            equals(android.os.Environment.MEDIA_MOUNTED)) {
        cacheDir = getApplicationContext().getExternalCacheDir();
        appDirectory = new File(cacheDir + subFolder);

    } else {
        cacheDir = getApplicationContext().getCacheDir();
        String BaseFolder = cacheDir.getAbsolutePath();
        appDirectory = new File(BaseFolder + subFolder);

    }

    if (appDirectory != null && !appDirectory.exists()) {
        appDirectory.mkdirs();
    }

    File fileName = new File(appDirectory, file);

    FileOutputStream fos = null;
    ObjectOutputStream out = null;
    try {
        fos = new FileOutputStream(fileName);
        out = new ObjectOutputStream(fos);
        out.writeObject(userSettings);
    } catch (IOException ex) {
        ex.printStackTrace();
    }  catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null)
                fos.flush();
            fos.close();
            if (out != null)
                out.flush();
            out.close();
        } catch (Exception e) {

        }
    }
}


public void readSetttings() {
    File cacheDir = null;
    File appDirectory = null;
    if (android.os.Environment.getExternalStorageState().
            equals(android.os.Environment.MEDIA_MOUNTED)) {
        cacheDir = getApplicationContext().getExternalCacheDir();
        appDirectory = new File(cacheDir + subFolder);
    } else {
        cacheDir = getApplicationContext().getCacheDir();
        String BaseFolder = cacheDir.getAbsolutePath();
        appDirectory = new File(BaseFolder + subFolder);
    }

    if (appDirectory != null && !appDirectory.exists()) return; // File does not exist

    File fileName = new File(appDirectory, file);

    FileInputStream fis = null;
    ObjectInputStream in = null;
    try {
        fis = new FileInputStream(fileName);
        in = new ObjectInputStream(fis);
        Map<String, String> myHashMap = (Map<String, String> ) in.readObject();
        userSettings = myHashMap;
        System.out.println("count of hash map::"+userSettings.size() + " " + userSettings);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (StreamCorruptedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }finally {

        try {
            if(fis != null) {
                fis.close();
            }
            if(in != null) {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Gho*_*ica 0

您的问题非常简单:您在写入数据时使用两个不同的文件名。阅读它。

FileOutputStream fos = context.openFileOutput("test.ser", Context.MODE_PRIVATE);
Run Code Online (Sandbox Code Playgroud)

FileInputStream fileInputStream = new FileInputStream(context.getFilesDir()+"test.ser");
Run Code Online (Sandbox Code Playgroud)

而且,很可能,您的阅读代码确实向您抛出了 IOException,告诉您有关尝试打开不存在的文件的信息。

因此,真正的要点/答案是:非常仔细地阅读这些异常消息。通常,他们会准确地告诉您问题是什么!