java属性hashmap读取

发布于 2024-12-09 07:36:33 字数 761 浏览 0 评论 0原文

回答 我刚刚找到了我正在寻找的东西:

Properties properties = new Properties();
FileInputStream in = new FileInputStream("/somePath/file.map");
properties.load(in);
in.close();
HashMap<String, String> propMap = new HashMap<String, String>((Map) properties);

这使我能够将打开的属性数据返回到哈希图中,而无需知道属性名称。

原始问题。

我有以下代码可以写出 HashMap 的结果。我想知道备份打开此属性并将数据拉回 HashMap 的最简单方法,putAll 是将数据获取到属性并存储数据的好方法。我没有看到 getAll 来检索它,并且在创建哈希映射之前不知道 HashMap 键/值数据,因此不能仅通过属性名称进行检索。创建后的数据将是静态的,因此我可以物理打开基于哈希图写入的文件以获取属性名称,但宁愿不必这样做。感谢您的任何帮助。

Properties properties = new Properties();
properties.putAll(mapTabSets);
properties.store(new FileOutputStream("/somePath/file.map"),"Java properties);

Answer
I just found what I was looking for:

Properties properties = new Properties();
FileInputStream in = new FileInputStream("/somePath/file.map");
properties.load(in);
in.close();
HashMap<String, String> propMap = new HashMap<String, String>((Map) properties);

This allowed me to get the opened property data back into the hashmap with out needing to know the property names.

original question.

I have the following code that writes out the results of a HashMap. I'm wondering the easiest way to open this property back up and pull the data back into a HashMap, the putAll was a nice way to get the data into the property and store the data. I don't see a getAll to retrieve it, and the HashMap Key/Value data was not known before the hashmap was created so can't just retrieve by property name. The data after it was created will be static, so I could physically open the file written based off the hashmap to get the property names, but would rather not have to do it that way. Thanks for any help.

Properties properties = new Properties();
properties.putAll(mapTabSets);
properties.store(new FileOutputStream("/somePath/file.map"),"Java properties);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二智少女猫性小仙女 2024-12-16 07:36:33

尽管属性没有 getAll 函数,但它们确实具有

propertynames()
stringPropertyNames()

这两个函数,这两个函数将提供属性的所有键的集合。然后,您可以迭代集合并通过以下方式从属性中提取所有值

properties.getProperty(String)

Although properties do not have a getAll function, they do have

propertynames()
stringPropertyNames()

both of which will deliver a collection of all keys of the property. You can then iterate over the collection and extract all values from the property via

properties.getProperty(String)
格子衫的從容 2024-12-16 07:36:33

尝试以下操作。

Properties properties = new Properties();
properties.load(new FileInputStream("/somePath/file.map"));

Try the following.

Properties properties = new Properties();
properties.load(new FileInputStream("/somePath/file.map"));
始终不够 2024-12-16 07:36:33
    package com.mkyong.common;

    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Properties;

    public class ClassName {

        private static HashMap<String, String> mymap = new HashMap<String, String>();

        public ClassName() throws IOException{
            Properties prop = new Properties();
            prop.load(ClassName.class.getClassLoader().getResourceAsStream("ini.properties"));

            mymap.put("1", prop.getProperty("required.firstName"));
            mymap.put("2", prop.getProperty("required.lastName"));
            mymap.put("3", prop.getProperty("equired.address"));
        }

        public static void main(String[] args) throws IOException {
            ClassName className = new ClassName();
            System.out.println("The Value of Key-1 ::" + mymap.get("1"));
        }
    }

Use the properties file:-
****************************
required.firstName=Nitesh
required.lastName=Rathore
required.address=DangeChowk
    package com.mkyong.common;

    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Properties;

    public class ClassName {

        private static HashMap<String, String> mymap = new HashMap<String, String>();

        public ClassName() throws IOException{
            Properties prop = new Properties();
            prop.load(ClassName.class.getClassLoader().getResourceAsStream("ini.properties"));

            mymap.put("1", prop.getProperty("required.firstName"));
            mymap.put("2", prop.getProperty("required.lastName"));
            mymap.put("3", prop.getProperty("equired.address"));
        }

        public static void main(String[] args) throws IOException {
            ClassName className = new ClassName();
            System.out.println("The Value of Key-1 ::" + mymap.get("1"));
        }
    }

Use the properties file:-
****************************
required.firstName=Nitesh
required.lastName=Rathore
required.address=DangeChowk
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文