讀取properties配置文件工具類

package com.ule.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;


public class PropertiesUtils {

    /**
     * (注意:加載的是src下的文件,如果在某個包下.請把包名加上)
     * @return
     */
    public static Properties getProperties(String sourcesFileName){
        Properties properties = new Properties();
        String savePath = Thread.currentThread().getContextClassLoader().getResource(sourcesFileName).getPath();
        try{
            InputStream inputStream = new BufferedInputStream(new FileInputStream(savePath));
            properties.load(inputStream);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  properties;
    }

    /**
     * 獲取屬性文件的數據 根據key獲取值
     * @param key
     * @return
     */
    public static String findPorpertyByKey(String key,String sourcesFileName){

        try {
            Properties pro = getProperties(sourcesFileName);
            return pro.getProperty(key);
        }catch (Exception e){
            return "";
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章