java类读取properties里内容

首先新建properties文件

 1 public class Properties {
 2     
 3 
 4     //定义配置文件名称
 5     public static final String PROPFILENAME_PROJECT = "config.properties";
 6 
 7     //pdf
 8     public static final String URL_PDFIMAGELOG = "url.pdfImageLogo";
 9     public static final String URL_CREATEPDF = "url.toCreatePdf";
10     public static final String URL_PDFFILE = "url.pdfFile";
11 
12     /**
13      * 根据配置文件名和key值获取value
14      * @param fileName
15      * @param key
16      * @return
17      * @throws Exception
18      */
19     public static String getValue(String fileName, String key) throws Exception {
20         return ReadPropertiesUtil.getValue(fileName, key);
21     }
22     /**
23     * @Description: 通过key从资源文件读取内容,并格式化
24     * @return String    返回类型 
25     * @author dongye 
26     * @date 2016年6月12日 下午2:16:12 
27     * @throws
28      */
29     public static String getValue(String fileName, String key, Object[] objs) throws Exception{
30         String pattern = getValue(fileName, key);
31         String value = MessageFormat.format(pattern, objs);
32         return value;
33     }
34 
35     //默认调用project.properties配置文件
36     public static String getValue(String key) throws Exception {
37         return ReadPropertiesUtil.getValue(PROPFILENAME_PROJECT, key);
38     }
39 
40 }
public class ReadPropertiesUtil {
    
    private static String propFilePath = "";
    private static Map<String, Object> configMap = new HashMap<String, Object>();
    
    public static void loadConfig(String propFilePath) throws Exception {
        if(PubMethod.isEmpty(configMap) || !configMap.containsKey(propFilePath)){
            Properties config = new Properties();
            config.load(ReadPropertiesUtil.class.getResourceAsStream(propFilePath));
            configMap.put(propFilePath, config);
        }
    }
    
    /**
     * 通过配置文件key值,获取对应配置值
     * @param key
     * @return
     * @throws Exception
     */
    public static String getValue(String filename,String key) throws Exception {
        Properties config;
        if (!PubMethod.isEmpty(configMap) && configMap.containsKey("/" + filename)) {
            config = (Properties) configMap.get("/" + filename);
            return config.getProperty(key).trim();
        }else {
            loadConfig("/" + filename);
            config = (Properties) configMap.get("/" + filename);
            return config.getProperty(key).trim();
        }
    }
    
}

 

转载于:https://www.cnblogs.com/snow1314/p/5695692.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值