java中读取配置文件

如果Java web项目,项目运行于tomcat或其余容器时,能够使用下面方式来获取文件的输入流java

一、当属性文件放在src下面时web

InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("test.properties");  
Properties p = new Properties();  
p.load(is);  

二、当属性文件放在某个包下面时,如:com.test.configapi

InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("test.properties");  
Properties p = new Properties();  
p.load(is);  

如果java项目,则使用下面方法读取属性文件tomcat

InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("com/test/config/test.properties");  
Properties p = new Properties();  
p.load(is);  

若属性文件放在某个包下面,则使用com/test/config/test.properties便可eclipse

 

*.properties格式的文件在Java中用的仍是挺多的,能够说写java项目时只要有配置数据基本都会使用*.properties来作,这也是java官方支持的一种配置文件,jdk提供了现成的api来操做。编辑器

在eclipse中使用properties格式的文件时,默认会将properties文件中的中文转成asc码,这时咱们能够给eclipse安装一个properties编辑器插件就能够支持中文显示了。.net

地址:http://blog.csdn.net/abc_key/article/details/46511259 ;插件

相关文章
相关标签/搜索