java读取properties文件

来源:互联网 发布:python免费视频教程 编辑:程序博客网 时间:2024/05/16 06:11
        InputStream is = this.getClass().getResourceAsStream("/" + propertyFileName + ".properties");

把文件以字节流的形式读入内存;

    Properties  props = new Properties();
        try {

     * Reads a property list (key and element pairs) from the input  byte stream.

     * The input stream is in a simple line-oriented format

         props.load(is);
        } catch (IOException e) {
            logger.error("加载properties配置文件错误:", e);
            //e.printStackTrace();
        }

Properties 继承了 HashTable;也是以键值对的形式存放数据的

再使用 Properties类中的getProperty方法获得需要的那个键值对的值

public String getProperty(String key) {
    Object oval = super.get(key);
    String sval = (oval instanceof String) ? (String)oval : null;
    return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
    }

原创粉丝点击