java属性哈希映射读取

java属性哈希映射读取,java,properties,hashmap,Java,Properties,Hashmap,回答 我刚刚找到了我想要的: 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); 尝试

回答 我刚刚找到了我想要的:

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);
尝试以下方法

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

虽然属性没有getAll函数,但它们确实有

propertynames()
stringPropertyNames()
两者都将提供属性的所有密钥的集合。然后,您可以遍历集合,并通过

properties.getProperty(String)
package com.mkyong.common;
导入java.io.IOException;
导入java.util.HashMap;
导入java.util.Properties;
公共类类名{
私有静态HashMap mymap=newhashmap();
public ClassName()引发IOException{
Properties prop=新属性();
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”);
}
公共静态void main(字符串[]args)引发IOException{
ClassName ClassName=新的ClassName();
System.out.println(“Key-1的值::”+mymap.get(“1”));
}
}
使用属性文件:-
****************************
必需。firstName=Nitesh
必需。lastName=Rathore
必需。地址=DangeChowk

谢谢,我在打开属性文件时没有遇到问题,只是不确定如何将其返回到我的hashmap中,但我找到了答案并在问题中添加了注释。需要注意的是属性是Hashtable的一个实现,从技术上讲,它是一个映射(具有键值对)。祝你好运,这帮了大忙。谢谢在获取数据时,它们的处理方式似乎有点不同,比如在for循环中,如果我理解了我正在阅读的内容,但这确实帮助我更好地理解它。谢谢。。这两个答案对我帮助很大,但我使用propertynames()将密钥作为一个集合来获取,所以请检查这个,但两个答案都有一个。
properties.getProperty(String)
    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