1. 程式人生 > >java中讀取配置文件

java中讀取配置文件

eclips current base ips extc eas lac true ava

若是Java web項目,項目運行於tomcat或其他容器時,可以使用下面方式來獲取文件的輸入流

1、當屬性文件放在src下面時

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

2、當屬性文件放在某個包下面時,如:com.test.config

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

若是java項目,則使用下面方法讀取屬性文件

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

若屬性文件放在某個包下面,則使用com/test/config/test.properties即可

*.properties格式的文件在Java中用的還是挺多的,可以說寫java項目時只要有配置數據基本都會使用*.properties來做,這也是java官方支持的一種配置文件,jdk提供了現成的api來操作。

在eclipse中使用properties格式的文件時,默認會將properties文件中的中文轉成asc碼,這時我們可以給eclipse安裝一個properties編輯器插件就可以支持中文顯示了。

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

java中讀取配置文件