Android 工作室商店地图 <String, Integer> 在 file.properties 具有属性

Android studio store map<String, Integer> in file.properties with properties

我正在尝试保存 file.properties。 我可以保存一张 地图。

但是当我尝试保存 地图时出现错误。

有没有办法保存 映射?

无效的代码:

    Map<String, Integer> int_map = new HashMap<String, Integer>();
    int_map.put("Red", 4);
    int_map.put("Orange-red", 3);
    int_map.put("Orange", 2);
    int_map.put("Green", 1);
    int_map.put("Blue", 0);

    Properties properties = new Properties(); // Crate properties object to store the data

    for (Map.Entry<String, Integer> entry : int_map.entrySet()) {
        properties.put(entry.getKey(), entry.getValue());
    }

    try {
        properties.store(new FileOutputStream(this.getFilesDir() + "data.properties"), null);
    } catch (IOException e) {
        e.printStackTrace();
    }

有效代码:

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

        map.put("hello", "world");
        
        Properties properties = new Properties(); // Crate properties object to store the data

        for (Map.Entry<String, String> entry : map.entrySet()) {
            properties.put(entry.getKey(), entry.getValue());
        }

        try {
            properties.store(new FileOutputStream(this.getFilesDir() + "data.properties"), null);
        } catch (IOException e) {
            e.printStackTrace();
        }

我尝试 运行 时遇到的错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.trialanderror, PID: 24139
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.trialanderror/com.example.trialanderror.MainActivity}: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3555)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3707)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2220)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:8016)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1087)
     Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
        at java.util.Properties.store0(Properties.java:836)
        at java.util.Properties.store(Properties.java:820)
        at com.example.trialanderror.MainActivity.onCreate(MainActivity.java:36)
        at android.app.Activity.performCreate(Activity.java:7957)
        at android.app.Activity.performCreate(Activity.java:7946)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3530)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3707) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2220) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:8016) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1087) 

这是设计使然。

documentation我们可以读到:

Because Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used instead. If the store or save method is called on a "compromised" Properties object that contains a non-String key or value, the call will fail.

(强调我的)。

换句话说,您只需为键和值存储 String 对象。如果您有 Integer,请将其转换为 String,然后将其写入 Properties。在你的情况下而不是

properties.put(entry.getKey(), entry.getValue());

你可能想使用类似

的东西
properties.setProperty(entry.getKey(), entry.getValue().toString());
//        |                                            ^-- convert Integer to String
//        |
//        ^--use `setProperty(String key, String value)` method instead of
//           `put(K key, V value)` to let compiler enforce String as key and value

或者如果 map 可以包含 null 值而不是获取 NPE 你想将它存储为字符串 "null" 使用 Objects.toString(entry.getValue()) 而不是 entry.getValue().toString().