Populating a HashMap with entries from a properties file

前端 未结 5 1192
南笙
南笙 2020-12-14 08:45

I want to populate a HashMap using the Properties class.
I want to load the entries in the .propeties file and then copy it into

5条回答
  •  無奈伤痛
    2020-12-14 09:45

    you can use the below Method to load the property file in a map

    public static Map getProperties(String filePropertyFile){
            Properties getProperties = new Properties();
            FileInputStream inputStream = null;
            HashMap propertyMap = new HashMap();
            try {
                inputStream = new FileInputStream(filePropertyFile);
                getProperties.load(inputStream);
                propertyMap.putAll((Map) getProperties);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return propertyMap;
        }
    

    Unit Test:

    @Test
    public void getPropertiesTest()
        String outputOmJarArrayAttributesPath = System.getProperty("user.dir") + "/src/main/resources/OutputOmJarArrayAttributes.properties";
        System.out.println(PropertyFileUtils.getProperties(outputOmJarArrayAttributesPath));;
        }
    

提交回复
热议问题