java读取properties配置文件

1、基于ClassLoder读取配置文件

注意:该方式只能读取类路径下的配置文件,有局限但是如果配置文件在类路径下比较方便。

Properties properties = new Properties();
//使用ClassLoader加载properties配置文件生成对应的输入流
InputStream in = PropertiesMain.class.getClassLoader().getResourceAsStream(config.properties);
// 使用properties对象加载输入流
properties.load(in);
//获取key对应的value值
String servers = properties.getProperty("xxx");

  

 

posted @ 2021-01-25 15:44  ASKANDANSWERS  阅读(109)  评论(0编辑  收藏  举报