循循渐进02篇 maven Properties 读取资源文件
Posted huhao2000
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了循循渐进02篇 maven Properties 读取资源文件相关的知识,希望对你有一定的参考价值。
Properties
<!--在pom build中配置resources,来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resouces</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
1.在java目录下properties
2.在resources目录下新建properties
发现:都被打包到了同一个路径下:classes 我们俗称这个路径为classpath
思路:需要一个文件流:
properties:
uersname=asdas
password=1asdasd
class:
public class Servletdome01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/hao/selever02/aa.properties");
Properties properties = new Properties();
properties.load(is);
String uersname = properties.getProperty("uersname");
String password = properties.getProperty("password");
resp.getWriter().print(uersname+password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
以上是关于循循渐进02篇 maven Properties 读取资源文件的主要内容,如果未能解决你的问题,请参考以下文章