首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

获取配置文件的几种模式

2012-12-22 
获取配置文件的几种方式public class PropertyUtil {/*** 得到Properties** @param path* @return*/public

获取配置文件的几种方式
public class PropertyUtil {

    /**
     * 得到Properties
     *
     * @param path
     * @return
     */
    public static Properties getProperties(String path) {
        if (path == null || path.equals(""))
            return null;
        Properties prop = new Properties();
        InputStream in;
        try {
            in = PropertyUtil.class.getClassLoader().getResourceAsStream(path);
            prop.load(in);
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        return prop;
    }
   
    /**
     * 得到给出键的属性值
     *
     * @param path
     * @param key
     * @return
     */
    public static String getProperty(String path, String key) {
        if (path == null || path.equals("") || key == null || key.equals(""))
            return null;
        Properties prop = new Properties();
        InputStream resource = System.class.getResourceAsStream(path);
        try {
            prop.load(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return prop.getProperty(key);
    }

    /**
     * 得到给定键的属性值
     *
     * @param resource
     * @param key
     * @return
     */
    public static String getProperty(InputStream resource, String key) {
        if (resource == null || key == null || key.equals(""))
            return null;
        Properties prop = new Properties();
        try {
            prop.load(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return prop.getProperty(key);
    }

}

热点排行