java读取Property文件

/**
 * @author roy
 * @version 1.0
 * created_dt:2020/09/01
 * property tool class
 */

import java.io.*;
import java.util.Properties;

public class PropsUtil {
    
    /**
     * @author roy
     * @version 1.0
     * @param String
     * @return Property 
     * created_dt:2020/09/01
     */
    public static Properties loadProps(String proFileName){
        Properties props = null;
        InputStream streamIn = null;
        try{
            streamIn = Thread.currentThread().getContextClassLoader().getResourceAsStream(proFileName);
            if(streamIn == null){
                throw new FileNotFoundException(proFileName + "file is not found");           
            }
            // 转一次流格式,解决中文乱码的问题
            BufferedReader readerBuff = new BufferedReader(new InputStreamReader(streamIn,"UTF-8"));
            props = new Properties(); 
            props.load(readerBuff);
        }catch(IOException e){
            LogHelper.getInstance().noteLog(PropsUtil.class.getName(),"load properties|error|" + e,"error");
        }finally{
            if(streamIn != null){
                try{
                    streamIn.close();
                }catch(IOException e){
                    LogHelper.getInstance().noteLog(PropsUtil.class.getName(),"close input|error|" + e,"error");
                }
            }
        }
        return props;
    }
    
    /**
     * @author roy
     * @version 1.0
     * @param Properties
     * @param String
     * @return String 
     * created_dt:2020/09/01
     * desc:读取property配置文件,当节点存在没有赋值时,默认值是空字符串(default "")
     */
    public static String getString(Properties proObjName,String proKeyName){
        return getString(proObjName,proKeyName,"");
    }
 
    /**
     * @author roy
     * @version 1.0
     * @param Properties
     * @param String
     * @param String
     * @return String 
     * created_dt:2020/09/01
     */
    public static String getString(Properties proObjName,String proKeyName,String deftValue){
        String reStr = deftValue;
        if(proObjName.containsKey(proKeyName)){
            reStr = proObjName.getProperty(proKeyName);
        }
        return reStr;
    }
    
    /**
     * @author roy
     * @version 1.0
     * @param Properties
     * @param String
     * @return int 
     * created_dt:2020/09/01
     */
    public static int getInt(Properties proObjName,String proKeyName){
        return getInt(proObjName,proKeyName,0);
        }
    

    /**
     * @author roy
     * @version 1.0
     * @param Properties
     * @param String
     * @param int
     * @return int 
     * created_dt:2020/09/01
     */
    public static int getInt(Properties proObjName,String proKeyName,int deftValue){
        int reInt = deftValue;
        if(proObjName.containsKey(proKeyName)){
            reInt = CastUtil.castInt(proObjName.getProperty(proKeyName));
        }
        return reInt;
    }
}

关注公众号“自然语言酷”,每周更新,从原生项目到spring boot,后端架构师进步之路。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值