Loading multiple properties files

后端 未结 6 1730
自闭症患者
自闭症患者 2021-02-12 11:25

Is it possible to stack loaded properties in Java? For instance can I do:

Properties properties = new Properties();

properties.load(new FileInputStream(\"file1.         


        
6条回答
  •  清歌不尽
    2021-02-12 12:07

    You can do this a little more dynamical, working with an indeterminate number of files.

    The parameter for this method should be a list with the path to the property file. I made the method static, put it on a class with other message handling related functions, and simply call it when I need it:

    public static Properties loadPropertiesFiles(LinkedList files) {
        try {
            Properties properties = new Properties();
    
                    for(String f:files) {
                        Resource resource = new ClassPathResource( f );
                        Properties tempProp = PropertiesLoaderUtils.loadProperties(resource);
                        properties.putAll(tempProp);
                    }
                    return properties;
        }
        catch(IOException ioe) {
                    return new Properties();
        }
    }
    

提交回复
热议问题