java读取properties文件内容

java读取properties文件内容,做网站的时候,经常会把一些基本配置,写到properties文件里。如果要修改这些基本配置,就只要修改此文件就行了,不用修改java文件。
下面是读取properties文件内容的代码:


import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;


import org.apache.log4j.Logger;


/**

* 读取Properties配置文件信息

* @author sunlightcs

* 2011-5-2

* http://hi.juziku.com/sunlightcs/

*/

public class PropertiesFile {

private static Logger logger = Logger.getLogger(PropertiesFile.class);



/**

* 读取Properties配置文件

* @return 返回Properties数据

*/

public Properties getPropertiesFile(){

InputStream is = this.getClass().getClassLoader().getResourceAsStream("config.properties");

Properties prop = new Properties();

try {

prop.load(is);

is.close();

} catch (IOException ex) {

logger.error("读取配置文件失败,配置文件路径为:config.properties", ex);

}

return prop;

}



/**

* 读取Properties配置文件

* @param filePath 配置文件路径

* @return 返回Properties数据

*/

public Properties getPropertiesFile(String filePath){

InputStream is = this.getClass().getClassLoader().getResourceAsStream(filePath);

Properties prop = new Properties();

try {

prop.load(is);

is.close();

} catch (IOException ex) {

logger.error("读取配置文件失败,配置文件路径为:"+filePath, ex);

}

return prop;

}







public static void main(String[] args){

Properties prop = new PropertiesFile().getPropertiesFile();

String path = prop.getProperty("imagePath");



System.out.println(path);

}

}




其中,有一个config.properties文件,放在classes目录里,文件内容如下:


#图片路径
imagePath=D:\\www\\imagePath
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值