java 加载properties

    /**
     * 取得属性值,无值时返回默认值
     * @param name String 属性名称
     * @param defaultValue String 属性名称
     * @return String 属性值
     */
    protected String getProp(String name,String defaultValue) {
          if (properties == null) {
            synchronized(propertiesLock) {
                if (properties == null) {
                    loadProps();
                }
            }
        }
        String property = properties.getProperty(name);
        if (property == null) {
            return defaultValue;
        }
        else {
            return property.trim();
        }
    }

 

/**
     * 加载属性
     */
    private void loadProps() {
        properties = new Properties();
        InputStream in = null;
        try {
            in = getClass().getResourceAsStream(resourceURI);
            properties.load(in);
        }
        catch (Exception e) {
            System.err.println("Error reading Application properties in PropertyManager.loadProps() " + e);
            e.printStackTrace();
        }
        finally {
            try {
                in.close();
            } catch (Exception e) { }
        }
    }
原文地址:https://www.cnblogs.com/whm-blog/p/10436512.html