android studio store map< string,integer>位于file.properties with properties中

u1ehiz5o  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(305)

我正在尝试保存file.properties。我可以保存一张<string,string>Map。
但是当我试图保存<string,integer>Map时,我得到了一个错误。
有没有办法保存<string,integer>Map?
不起作用的代码:

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) 
bvuwiixz

bvuwiixz1#

这是精心设计的。
从文档中我们可以看到:
因为 Properties 继承自 Hashtable ,的 put 以及 putAll 方法可以应用于属性对象。强烈反对使用它们,因为它们允许调用者插入键或值不是字符串的条目。这个 setProperty 应该改用方法。如果 store 或者 save 方法,则调用将失败。
(我的)。
换句话说,您只需要存储键和值的字符串对象。如果有整数,请将其转换为字符串,然后将其写入属性。在你的情况下而不是

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() .

相关问题