码迷,mamicode.com
首页 > 编程语言 > 详细

java中读取配置文件

时间:2017-05-16 20:06:47      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:方式   log   属性   blog   中文   context   nbsp   replace   配置文件   

若是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中读取配置文件

标签:方式   log   属性   blog   中文   context   nbsp   replace   配置文件   

原文地址:http://www.cnblogs.com/catkins/p/6863114.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!