使用Properties对象获取.properties配置文件中的值

1.在项目中新建一个constant.propertis文件,并设置username=root

2.创建一个java类,代码如下:

public class ConstantUtil {


private static Properties p = new Properties();

static {

        InputStream inputStream = ConstantUtil.class.getClassLoader().getResourceAsStream

                ("constant.properties");   

try {   
p.load(inputStream);   
} catch (IOException e1) {   
e1.printStackTrace();  
}   

}

        public static String USER_NAME= p.getProperty("username");

}

3,创建测试类

public class Test(){

    public static void main(String[] args){

           System.out.println(ConstantUtil.USER_NAME);

    }

}

你可能感兴趣的:(使用Properties对象获取.properties配置文件中的值)