读取properties文件, 比如获得税率,提示等信息
package com.appbox.util.property;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
public class PropertiesUtil {
//根据key读取value
private static Logger log = Logger.getLogger(PropertiesUtil.class.getName());
/**
* 默认配置文件名称
*/
private static String defaultPropertyName="advertise.properties";
/**
* 从默认的配置文件中按key取value
* @param key
* @return
*/
public static String getPropertyValue(String key){
return getProperties(defaultPropertyName,key);
}
/**
*
* @param filePath property的文件名字
* @param key key值
* @return value值
*/
public static String getProperties(String filePath,String key){
String value="";
try{
InputStream in=PropertiesUtil.class.getClassLoader().getResourceAsStream(filePath);
Properties p=new Properties();
p.load(in);
value=p.getProperty(key);
in.close();
p=null;
}catch(Exception e){
log.error("读取配置文件错误:getProperties(String filePath,String key):"+e.getMessage());
e.printStackTrace();
}
return value;
}
}
.properties文件
format.number = {0,number,0.00}
format.number2 = 101
struts.messages.error.content.type.not.allowed=上传的文件类型不符,只允许bmp,gif,jpg,png格式
struts.messages.error.file.too.large=文件上传失败,上传的文件太大
struts.messages.error.uploading=文件上传失败,发生内部错误
producturl=http\://lil\:8080/app/openproduct/show.action?productid\=
configroot=automake/config
templateroot=automake/template
taobaotemplate=automake/taobaotemplate
clientfileroot=automake/clientfile
sina_key=1072318004
sina_secret=2f06daa9b148d7085c0eaec6254e3204
taobao_apidomin=http\://gw.api.taobao.com/router/rest
advertisernotifymoney=100
developernotifymoney=100
idcardimagepath=idcard
invalid.fieldvalue.mediasetting.jifenrateAd=积分汇率只能为数字
invalid.fieldvalue.mediasetting.minjifenAd=积分数量只能为整型数字
point=0.3
taxrate=0.06
point1=0.2
jifentocpa=100
pricetocpm=100
pricetocpa=10000000
pricetocpc=1000000
java中使用方法
private String idcardimagepath = PropertiesUtil.getPropertyValue("idcardimagepath");