How to read properties file from meta-inf folder f

2019-08-08 03:41发布

How to read properties file from meta-inf folder in a web application from plain java class.

2条回答
祖国的老花朵
2楼-- · 2019-08-08 04:33

The simplest you can do is :-

InputStream propertiesIs = this.getClass().getClassLoader().getResourceAsStream("META-INF/your.properties");
Properties prop = new Properties();
prop.load(propertiesIs);
System.out.println(prop.getProperty(YourPropertyHere));

OR

you can try loading your properties using FileInputStream also :-

input = new FileInputStream("META-INF/your.properties");
查看更多
混吃等死
3楼-- · 2019-08-08 04:37
package net.bounceme.doge.json;

import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PropertiesReader {

    private static final long serialVersionUID = 1L;
    private static final Logger log = Logger.getLogger(PropertiesReader.class.getName());

    public Properties tryGetProps(String propertiesFileName) {
        log.fine(propertiesFileName);
        Properties properties = new Properties();
        try {
            properties = getProps(propertiesFileName);
        } catch (IOException ex) {
            Logger.getLogger(PropertiesReader.class.getName()).log(Level.SEVERE, null, ex);
        }
        log.info(properties.toString());
        return properties;
    }

    private Properties getProps(String propertiesFileName) throws IOException {
        log.fine(propertiesFileName);
        Properties properties = new Properties();
        properties.load(PropertiesReader.class.getResourceAsStream("/META-INF/" + propertiesFileName + ".properties"));
        log.fine(properties.toString());
        return properties;
    }
}

this works for me.

查看更多
登录 后发表回答