获得配置文件键值对application.properties

来源:互联网 发布:现在什么软件好 编辑:程序博客网 时间:2024/06/05 18:30

获取配置文件里面的键值对

可保证路径在Tomcat中找到

package util;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.net.URL;import java.util.Properties;/** * <p> * ClassName: ConfigUtil * </p> * <p> * Description: 配置文件 * </p> * <p> * Author: hyson * </p> * <p> * Date: 2014-09-29 * </p> */public class ConfigUtil {    /**     * 获得值     *      * @param key 键     * @return 值     */    public static String getConfig(String key) {        Properties p;        p = new Properties();        FileInputStream fis = null;        URL url;        url = ConfigUtil.class.getClassLoader().getResource("application.properties");        try {            fis = new FileInputStream(url.getPath());            p.load(fis);        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (fis != null) {                try {                    fis.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        return p.getProperty(key);    }}

0 0
原创粉丝点击