在java类中获取在.properties配置文件中设置的参数

2014-11-24 08:51:32 · 作者: · 浏览: 0

如何获取.properties配置文件中的参数,我在网上查了半天没弄明白,后来在以前的项目中找到了,就写下来,避免遗忘。

1.配置文件:message_product.properties

01
total_product=2
02
service1=1,新股发行短信提醒服务,发行的详细信息
03
service2=2,配股短信提醒服务&增发短信提醒服务,增发的详细信息
04

05
validatecode_temp=验证码:${vilidateCode}。
06

07
#消息持续发送时间,单位”小时“
08
message_constant_time=24
09

10
#消息的编码格式 15 GBK编码 30 彩信 31 wappush 32 长短信 33个性彩信'
11
message_formart=15
12

13
#'消息的优先级: 0 最低 --- 3 最高';
14
message_priority=3
15

16
#'消息的下发类型: 0 免费下发 1 按条下发 2 包月下发 3 订阅请求 4 取消请求 5 包月扣费';
17
message_type=0
18

19
#产品ID numproductid
20
message_product_id=724
21
#numchannelid
22
message_channelid=0
2.java 类
01
package com.margin.utils;
02

03
import java.io.UnsupportedEncodingException;
04
import java.util.ResourceBundle;
05

06
import com.margin.po.MessageProduct;
07

08
public class MessageProperties {
09
private static ResourceBundle rb;
10

11
static {
12
rb = ResourceBundle.getBundle("message_product");
13
}
14

15
/**
16
* 得到短信服务总数
17
* @return
18
*/
19
public static int getTotalProduct() {
20
return Integer.parseInt(rb.getString("total_product"));
21
}
view sourceprint
01
/**
02
* 消息持续发送时间
03
* @return
04
*/
05
public static int getMessageConstantTime()
06
{
07
return Integer.parseInt(rb.getString("message_constant_time").trim());
08
}
09

10
/**
11
* 消息格式
12
* @return
13
*/
14
public static int getMessageFormat()
15
{
16
return Integer.parseInt(rb.getString("message_formart").trim());
17
}
18

19
/**
20
* 消息发送优先级
21
* @return
22
*/
23
public static int getMessagePriority()
24
{
25
return Integer.parseInt(rb.getString("message_priority").trim());
26
}
作者:叶知秋