java讀取配置文件

private static final String RES_FILE_PATH ="resources_file.properites"; private int readPropertiesValue(final String key) private int readPropertiesValue(final String key) throws IOException {
        InputStream inputStream = null;
        final Properties props = new Properties();
        try {
            inputStream = Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream(RES_BACKYARD_FILE);
//            inputStream = this.getClass().getClassLoader().getResourceAsStream(RES_BACKYARD_FILE);

//            這個方法不能使用在 靜態模塊中獲取ClassLoader。

            props.load(inputStream);
            final String valueStr = props.getProperty(key);
            int returnValue = 0;
            if (valueStr != null) {
                returnValue = Integer.valueOf(valueStr);
            }
            return returnValue;
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }

發佈了36 篇原創文章 · 獲贊 1 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章