javaweb中读取配置文件

读取javaweb中配置文件的方式,一般来说,主要有如下三种:

一:通过ServletContext读取配置文件

二:通过类加载器读取配置文件

三:通过文件的绝对路径,然后通过传统的方式得到配置文件

假设web工程中有如下不同位置的配置文件,我们如何读取呢?

一:通过ServletContext读取配置文件

代理示例如下:

[java]  view plain  copy
  1. package cn.ccnu.test;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.util.Properties;  
  7.   
  8. import javax.servlet.ServletException;  
  9. import javax.servlet.http.HttpServlet;  
  10. import javax.servlet.http.HttpServletRequest;  
  11. import javax.servlet.http.HttpServletResponse;  
  12.   
  13. public class ReadProperties extends HttpServlet {  
  14.   
  15.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  16.             throws ServletException, IOException {  
  17.         //使用ServletContext读取配置文件(使用配置文件的相对路径)  
  18.         //注:getResourceAsStream中配置文件的路径的写法为:web工程发布到服务器后所对应的目录,是一个相对路径  
  19.           
  20.         //1,读取src目录下的配置文件,因为web工程发布到服务器后的src.properties的相对路径为:/WEB-INF/classes/src.properties  
  21.         InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/src.properties");  
  22.         Properties prop = new Properties();  
  23.         prop.load(in);  
  24.         String url = prop.getProperty("url");  
  25.         System.out.println(url);  
  26.           
  27.         //2,读取包cn.ccnu目录下的配置文件,因为web工程发布到服务器后的cnccnu.properties的相对路径为:/WEB-INF/classes/cn/ccnu/cnccnu.properties  
  28.         InputStream in2 = this.getServletContext().getResourceAsStream("/WEB-INF/classes/cn/ccnu/cnccnu.properties");  
  29.         Properties prop2 = new Properties();  
  30.         prop2.load(in2);  
  31.         String ur2 = prop2.getProperty("url");  
  32.         System.out.println(ur2);  
  33.                   
  34.         //3,读取webRoot目录下的配置文件,因为web工程发布到服务器后的webroot.properties的相对路径为:/webroot.properties  
  35.         InputStream in3 = this.getServletContext().getResourceAsStream("/webroot.properties");  
  36.         Properties prop3 = new Properties();  
  37.         prop3.load(in3);  
  38.         String ur3 = prop3.getProperty("url");  
  39.         System.out.println(ur3);  
  40.           
  41.         //使用ServletContext读取配置文件(使用配置文件的绝对路径)  
  42.         //注:getRealPath中的参数依旧是web工程发布到服务器后所对应的目录,是一个相对路径  
  43.         //1,读取src目录下的配置文件  
  44.         String path = this.getServletContext().getRealPath("/WEB-INF/classes/src.properties");  
  45.         FileInputStream fin = new FileInputStream(path);  
  46.         Properties prop4 = new Properties();  
  47.         prop4.load(fin);  
  48.         String ur4 = prop4.getProperty("url");  
  49.         System.out.println(ur4);  
  50.         //2,读取包cn.ccnu目录下的配置文件  
  51.         String path2 = this.getServletContext().getRealPath("/WEB-INF/classes/cn/ccnu/cnccnu.properties");  
  52.         FileInputStream fin2 = new FileInputStream(path2);  
  53.         Properties prop5 = new Properties();  
  54.         prop5.load(fin2);  
  55.         String ur5 = prop5.getProperty("url");  
  56.         System.out.println(ur5);  
  57.         //3,读取webRoot目录下的配置文件  
  58.         String path3 = this.getServletContext().getRealPath("/webroot.properties");  
  59.         FileInputStream fin3 = new FileInputStream(path3);  
  60.         Properties prop6 = new Properties();  
  61.         prop6.load(fin3);  
  62.         String ur6 = prop6.getProperty("url");  
  63.         System.out.println(ur6);  
  64.     }  
  65.   
  66.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  67.             throws ServletException, IOException {  
  68.         doGet(request, response);  
  69.     }  
  70.   
  71. }  

二:通过类加载器读取资源文件

示例代码如下:
[java]  view plain  copy
  1. //通过类加载器读取配置文件(相对路径,只能读取src下的配置文件)  
  2.         //1,读取src目录下的配置文件   注意,不要加/  
  3.         InputStream in = this.getClass().getClassLoader().getResourceAsStream("src.properties");  
  4.         Properties prop = new Properties();  
  5.         prop.load(in);  
  6.         String url = prop.getProperty("url");  
  7.         System.out.println(url);  
  8.         //2,读取包cn.ccnu目录下的配置文件  
  9.         InputStream in2 = this.getClass().getClassLoader().getResourceAsStream("cn/ccnu/cnccnu.properties");  
  10.         Properties prop2 = new Properties();  
  11.         prop2.load(in2);  
  12.         String ur2 = prop2.getProperty("url");  
  13.         System.out.println(ur2);  
  14.           
  15.         //通过类加载器读取配置文件(绝对路径)  
  16.         //1,读取src目录下的配置文件  
  17.         String path = this.getClass().getClassLoader().getResource("src.properties").getPath();  
  18.         FileInputStream fin1 = new FileInputStream(path);  
  19.         Properties prop3 = new Properties();  
  20.         prop3.load(fin1);  
  21.         String ur3 = prop3.getProperty("url");  
  22.         System.out.println(ur3);  
  23.         //2,读取包cn.ccnu目录下的配置文件  
  24.         String path2 = this.getClass().getClassLoader().getResource("cn/ccnu/cnccnu.properties").getPath();  
  25.         FileInputStream fin2 = new FileInputStream(path2);  
  26.         Properties prop4 = new Properties();  
  27.         prop4.load(fin2);  
  28.         String ur4 = prop4.getProperty("url");  
  29.         System.out.println(ur4);  


第三种方式,通过得到配置文件的绝对路径,然后得到配置文件,和上面讲解的差不多。

猜你喜欢

转载自blog.csdn.net/xiaobaixiongxiong/article/details/79359774