Maven组织的web项目读取WEB-INF下properties文件

来源:互联网 发布:模拟现场灯光软件 编辑:程序博客网 时间:2024/06/05 15:16

开发时经常要读取properties文件的配置信息,但是properties文件所在的位置和properties访问方式不同读取方式也不同,现就读取properties文件进行总结。

1、访问方式一般分:java项目和web项目。

2、文件位置:与源文件相同目录和与源目录不相同

  • java项目与源文件相同目录读取properties文件方法,在main函数中读取

[javascript] view plain copy
  1. import java.io.FileInputStream;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.Properties;  
  6.   
  7. public class PropertiesUtils {  
  8.       
  9.     public static void main(String[] args) {  
  10.         Properties properties = new Properties();  
  11.         try {  
  12.             InputStream in = PropertiesUtils.class.getResourceAsStream("./db.properties");//或者直接写成"db.properties"  
  13.             properties.load(in);  
  14.             String url = properties.getProperty("url");  
  15.             String userName = properties.getProperty("user");  
  16.             String password = properties.getProperty("password");  
  17.             System.out.println("url="+url+",userName="+userName+",password="+password);  
  18.         } catch (FileNotFoundException e) {  
  19.             e.printStackTrace();  
  20.         } catch (IOException e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.     }  
  24.   
  25. }  

父级或子级目录只要加相应的方法路径即可。读取properties文件的关键是能够访问到properties文件,读取的方法是一样的,主要在于访问的方法不一样。


这里有两个获取当前线程类路径根目录的方法,分别为:

Thread.currentThread().getContextClassLoader().getResource("")和ClassLoader.getSystemResource("")

两个方法作用一样,通过以上方法获取活动.classes文件的目录绝对位置,然后接受需要访问的路径即可。这两个方法不使用在web功能webapp下的文件方法,因为webapp下的文件不会出现在classes路径下。


  • maven组织的web项目中读取properties文件方法

以下方法是在http请求情况下访问的,部署服务器为tomcat

获取部署在tomcat下的真实地址进行拼接访问,

    • 绝对地址访问方式:

[javascript] view plain copy
  1. public Map<String, String> getDBInfo() {  
  2.     Map<String, String> dbInfo = new HashMap<String, String>();  
  3.     try {  
  4.         String url = this.getClass().getResource("").getPath();    
  5.            String path = url.substring(0, url.indexOf("WEB-INF")) + "WEB-INF/db.properties";    
  6.            Properties config = new Properties();    
  7.            InputStream in = new FileInputStream(path);  
  8.            config.load(in);   
  9.            String dbUrl = config.getProperty("url");  
  10.            dbInfo.put("url", dbUrl);  
  11.            dbInfo.put("userName", config.getProperty("user"));  
  12.            dbInfo.put("password", config.getProperty("password"));  
  13.     } catch (Exception e) {  
  14.         e.printStackTrace();  
  15.     }  
  16.     return dbInfo;  
  17.       
  18. }  

    • Servlet下的方法

[javascript] view plain copy
  1. public Map<String, String> getDBInfo() {  
  2.     Map<String, String> dbInfo = new HashMap<String, String>();  
  3.     try {  
  4.            String path = getServletContext().getRealPath("");    
  5.            Properties config = new Properties();    
  6.            InputStream in = new FileInputStream(path+"/WEB-INF/db.properties");  
  7.            config.load(in);   
  8.            String dbUrl = config.getProperty("url");  
  9.            dbInfo.put("url", dbUrl);  
  10.            dbInfo.put("userName", config.getProperty("user"));  
  11.            dbInfo.put("password", config.getProperty("password"));  
  12.     } catch (Exception e) {  
  13.         e.printStackTrace();  
  14.     }  
  15.     return dbInfo;  
  16.       
  17. }  

    • Servlet下的方法,相对地址访问方式:

[javascript] view plain copy
  1. public Map<String, String> getDBInfo() {  
  2.     Map<String, String> dbInfo = new HashMap<String, String>();  
  3.     try {  
  4.            Properties config = new Properties();    
  5.            InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/db.properties");  
  6.            config.load(in);   
  7.            String dbUrl = config.getProperty("url");  
  8.            dbInfo.put("url", dbUrl);  
  9.            dbInfo.put("userName", config.getProperty("user"));  
  10.            dbInfo.put("password", config.getProperty("password"));  
  11.     } catch (Exception e) {  
  12.         e.printStackTrace();  
  13.     }  
  14.     return dbInfo;  
  15.       
  16. }  


以上两种区别主要在于getResourceAsStream和getResource。所有的方法主要的思路在于首先得到CLASS路径值,即当前类.class文件所有的路径,然后在此路径的基础上通过父级、子级目录或项目的根目录开展遍历,java工程和web工程所在的路径是不一样的。掌握获取路径的方法后,其他事就水到渠成了。


附录:

Java中getResourceAsStream的用法:
首先,Java中的getResourceAsStream有以下几种: 
1. Class.getResourceAsStream(String path) : path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。其只是通过path构造一个绝对路径,最终还是由ClassLoader获取资源。

2. Class.getClassLoader.getResourceAsStream(String path) :默认则是从ClassPath根下获取,path不能以’/'开头,最终是由ClassLoader获取资源。

3. ServletContext. getResourceAsStream(String path):默认从WebAPP根目录下取资源,Tomcat下path是否以’/'开头无所谓,当然这和具体的容器实现有关。

getResourceAsStream 用法大致有以下几种:
第一: 要加载的文件和.class文件在同一目录下,例如:com.demo 下有类Test.class ,同时有资源文件myfile.xml
那么,应该有如下代码:
Test.class.getResourceAsStream("myfile.xml");

第二:在me.class目录的子目录下,例如:com.demo 下有类Test.class ,同时在com.demo.file 目录下有资源文件myfile.xml
那么,应该有如下代码:
Test.class.getResourceAsStream("file/myfile.xml");
或者如下写法:
Test.class.getResourceAsStream("./file/myfile.xml");
第三:不在Test.class目录下,也不在子目录下,例如:com.demo 下有类Test.class ,同时在 com.demo.file 目录下有资源文件myfile.xml
那么,应该有如下代码:
Test.class.getResourceAsStream("/com/demo/file/myfile.xml");


总结一下,可能只是两种写法
第一:前面有 “/”
“ / ”代表了工程的根目录,例如工程名叫做myproject,“ / ”代表了myproject
Test.class.getResourceAsStream("/com/demo/file/myfile.xml");

第二:前面没有 “/”,代表当前类的目录(当前路径也可以用“./”表示)
Test.class.getResourceAsStream("myfile.xml");
Test.class.getResourceAsStream("file/myfile.xml");
或者如下写法:
Test.class.getResourceAsStream("./myfile.xml");
Test.class.getResourceAsStream("./file/myfile.xml");



http://blog.csdn.net/wpydaguan/article/details/45971649

阅读全文
0 0
原创粉丝点击