java get content profile (properties) of

The following presentation java code to obtain the contents of the configuration file

Profiles content (in the src directory):

 

 

Code: 

import java.io.IOException;
import java.util.Properties;
 
public class getProperties {
    private static Properties properties = null;
    // 初始化
    static {
        properties = new Properties();
        try {
            properties.load(getProperties.class.getClassLoader().getResourceAsStream("./myConfig.properties"));
        }catch(IOException e) {
            throw new RuntimeException(e.getMessage(),e);
        }
    }
    // 获取值
    public static String getValue(String key) {
        return properties.getProperty(key);
    }
    public static void main(String[] args) {
        String value = getProperties.getValue("username");
        System.out.print("username="+value);
    }
}
 

operation result:

 

Guess you like

Origin www.cnblogs.com/jumpkin1122/p/11503458.html