javaweb中读取配置文件
读取javaweb中配置文件的方式,一般来说,主要有如下三种:
一:通过ServletContext读取配置文件
二:通过类加载器读取配置文件
三:通过文件的绝对路径,然后通过传统的方式得到配置文件
假设web工程中有如下不同位置的配置文件,我们如何读取呢?
一:通过ServletContext读取配置文件
代理示例如下:
- package cn.ccnu.test;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class ReadProperties extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- //使用ServletContext读取配置文件(使用配置文件的相对路径)
- //注:getResourceAsStream中配置文件的路径的写法为:web工程发布到服务器后所对应的目录,是一个相对路径
- //1,读取src目录下的配置文件,因为web工程发布到服务器后的src.properties的相对路径为:/WEB-INF/classes/src.properties
- InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/src.properties");
- Properties prop = new Properties();
- prop.load(in);
- String url = prop.getProperty("url");
- System.out.println(url);
- //2,读取包cn.ccnu目录下的配置文件,因为web工程发布到服务器后的cnccnu.properties的相对路径为:/WEB-INF/classes/cn/ccnu/cnccnu.properties
- InputStream in2 = this.getServletContext().getResourceAsStream("/WEB-INF/classes/cn/ccnu/cnccnu.properties");
- Properties prop2 = new Properties();
- prop2.load(in2);
- String ur2 = prop2.getProperty("url");
- System.out.println(ur2);
- //3,读取webRoot目录下的配置文件,因为web工程发布到服务器后的webroot.properties的相对路径为:/webroot.properties
- InputStream in3 = this.getServletContext().getResourceAsStream("/webroot.properties");
- Properties prop3 = new Properties();
- prop3.load(in3);
- String ur3 = prop3.getProperty("url");
- System.out.println(ur3);
- //使用ServletContext读取配置文件(使用配置文件的绝对路径)
- //注:getRealPath中的参数依旧是web工程发布到服务器后所对应的目录,是一个相对路径
- //1,读取src目录下的配置文件
- String path = this.getServletContext().getRealPath("/WEB-INF/classes/src.properties");
- FileInputStream fin = new FileInputStream(path);
- Properties prop4 = new Properties();
- prop4.load(fin);
- String ur4 = prop4.getProperty("url");
- System.out.println(ur4);
- //2,读取包cn.ccnu目录下的配置文件
- String path2 = this.getServletContext().getRealPath("/WEB-INF/classes/cn/ccnu/cnccnu.properties");
- FileInputStream fin2 = new FileInputStream(path2);
- Properties prop5 = new Properties();
- prop5.load(fin2);
- String ur5 = prop5.getProperty("url");
- System.out.println(ur5);
- //3,读取webRoot目录下的配置文件
- String path3 = this.getServletContext().getRealPath("/webroot.properties");
- FileInputStream fin3 = new FileInputStream(path3);
- Properties prop6 = new Properties();
- prop6.load(fin3);
- String ur6 = prop6.getProperty("url");
- System.out.println(ur6);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
- }
二:通过类加载器读取资源文件
示例代码如下:
- //通过类加载器读取配置文件(相对路径,只能读取src下的配置文件)
- //1,读取src目录下的配置文件 注意,不要加/
- InputStream in = this.getClass().getClassLoader().getResourceAsStream("src.properties");
- Properties prop = new Properties();
- prop.load(in);
- String url = prop.getProperty("url");
- System.out.println(url);
- //2,读取包cn.ccnu目录下的配置文件
- InputStream in2 = this.getClass().getClassLoader().getResourceAsStream("cn/ccnu/cnccnu.properties");
- Properties prop2 = new Properties();
- prop2.load(in2);
- String ur2 = prop2.getProperty("url");
- System.out.println(ur2);
- //通过类加载器读取配置文件(绝对路径)
- //1,读取src目录下的配置文件
- String path = this.getClass().getClassLoader().getResource("src.properties").getPath();
- FileInputStream fin1 = new FileInputStream(path);
- Properties prop3 = new Properties();
- prop3.load(fin1);
- String ur3 = prop3.getProperty("url");
- System.out.println(ur3);
- //2,读取包cn.ccnu目录下的配置文件
- String path2 = this.getClass().getClassLoader().getResource("cn/ccnu/cnccnu.properties").getPath();
- FileInputStream fin2 = new FileInputStream(path2);
- Properties prop4 = new Properties();
- prop4.load(fin2);
- String ur4 = prop4.getProperty("url");
- System.out.println(ur4);