获取properties配置文件中的内容
- 第一种
获取ServletContext对象
ServletContext context = getServletContext();
创建属性对象
Properties properties = new Properties();
获取资源,转化为流对象
InputStream is = context.getResourceAsStream(“config.properties”);//工程的绝对路径
properties.load(is);
获取配置文件内容
String name = properties.getProperty(“name”);
is.closer();
- 第二种
创建属性对象
Properties properties = new Properties();
获取资源,转化为流对象
InputStream is = this.getClass.getClassLoader().getResourceAsStream(“config.properties”);//工程的绝对路径
properties.load(is);
获取配置文件内容
String name = properties.getProperty(“name”);
is.closer();
注意:
两种方式获取的绝对路径不同
- 第一种的的路径对tomcat对应的项目路径下
- 第二种对应的路径为项目下的WEB-INF\classes路径