Loading multiple properties files

后端 未结 6 1745
自闭症患者
自闭症患者 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<String> 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();
        }
    }
    
    0 讨论(0)
  • 2021-02-12 12:09

    Actually, yes. You can do this. If any of the properties overlap, the newer loaded property will take place of the older one.

    0 讨论(0)
  • 2021-02-12 12:14

    This should also work. If same property is defined in file1.properties and file2.properties, property in file2.properties will be in effect.

        Properties properties = new Properties();
        properties.load(new FileInputStream("file1.properties"));
        properties.load(new FileInputStream("file2.properties"));
    

    Now the properties map will have properties from both files. If same key appears in file1 and file2, the value of the key from file1 will be updated in properties with value in file2 since I'm calling file1 then file2.

    0 讨论(0)
  • 2021-02-12 12:18

    You can do this:

    Properties properties = new Properties();
    
    properties.load(new FileInputStream("file1.properties"));
    
    Properties properties2 = new Properties();
    properties2.load(new FileInputStream("file2.properties"));
    
    properties.putAll(properties2);
    

    NOTE : All the keys maintained are unique. So, the later properties loaded with same key will be overridden. Just to keep for your ref :)

    0 讨论(0)
  • 2021-02-12 12:20

    Yes the properties stack. Properties extends Hashtable and load() simply calls put() on each key-value pair.

    Relevant code from the Source:

    String key = loadConvert(lr.lineBuf, 0, keyLen, convtBuf); 
    String value = loadConvert(lr.lineBuf, valueStart, limit - valueStart, convtBuf); 
    put(key, value); 
    

    In other words, loading from a file doesn't clear the current entries. However, note that if the two files contain entries with the same key, the first one will be overwritten.

    0 讨论(0)
  • 2021-02-12 12:23

    Yes, you need to pass the default properties file in the constructor. Like this you can chain them up.

    E.g.:

    Properties properties1 = new Properties();
    try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("file1.properties"))){
        properties1.load(bis);
    }
    
    Properties properties2 = new Properties(properties1);
    try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("file2.properties"))){
        properties2.load(bis);
    }
    
    0 讨论(0)
提交回复
热议问题