首页 > 代码库 > java 读取配置文件工具类 (how to read values from properties file in java)

java 读取配置文件工具类 (how to read values from properties file in java)

Java 读取配置文件工具类

使用 java.util.Properties 

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesReader {

    private static Properties prop;

    static {
        reload();
    }

    private static void reload() {
        prop = new Properties();

        try {
            InputStream inputStream = PropertiesReader.class.getClassLoader().getResourceAsStream("config.properties");

            prop.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取指定的系统属性值
     *
     * @param key
     * @return
     */
    public static String getProperty(String key) {

        return prop.getProperty(key);
    }

    /**
     * 获取指定的系统属性值(带默认值)
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getProperty(String key, String defaultValue) {

        return prop.getProperty(key, defaultValue);
    }

}

 

java 读取配置文件工具类 (how to read values from properties file in java)