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

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

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

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

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

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesUtils {
	
	public static void main(String[] args) {
		Properties properties = new Properties();
		try {
			InputStream in = PropertiesUtils.class.getResourceAsStream("./db.properties");//或者直接写成"db.properties"
			properties.load(in);
			String url = properties.getProperty("url");
			String userName = properties.getProperty("user");
			String password = properties.getProperty("password");
			System.out.println("url="+url+",userName="+userName+",password="+password);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

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


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

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

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


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

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

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

    • 绝对地址访问方式:

	public Map<String, String> getDBInfo() {
		Map<String, String> dbInfo = new HashMap<String, String>();
		try {
			String url = this.getClass().getResource("").getPath();  
            String path = url.substring(0, url.indexOf("WEB-INF")) + "WEB-INF/db.properties";  
            Properties config = new Properties();  
            InputStream in = new FileInputStream(path);
            config.load(in); 
            String dbUrl = config.getProperty("url");
            dbInfo.put("url", dbUrl);
            dbInfo.put("userName", config.getProperty("user"));
            dbInfo.put("password", config.getProperty("password"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dbInfo;
		
	}

    • Servlet下的方法

	public Map<String, String> getDBInfo() {
		Map<String, String> dbInfo = new HashMap<String, String>();
		try {
            String path = getServletContext().getRealPath("");  
            Properties config = new Properties();  
            InputStream in = new FileInputStream(path+"/WEB-INF/db.properties");
            config.load(in); 
            String dbUrl = config.getProperty("url");
            dbInfo.put("url", dbUrl);
            dbInfo.put("userName", config.getProperty("user"));
            dbInfo.put("password", config.getProperty("password"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dbInfo;
		
	}

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

	public Map<String, String> getDBInfo() {
		Map<String, String> dbInfo = new HashMap<String, String>();
		try {
            Properties config = new Properties();  
            InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/db.properties");
            config.load(in); 
            String dbUrl = config.getProperty("url");
            dbInfo.put("url", dbUrl);
            dbInfo.put("userName", config.getProperty("user"));
            dbInfo.put("password", config.getProperty("password"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dbInfo;
		
	}


以上两种区别主要在于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");



  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值