java读取 properties配置文件

主要有两种:

1,通过java.util.ResourceBundle类来读取

/**
     * 读取properties文件
     * @param proName 文件名(如果是放在src下,直接文件名即可)
     * @param params  参数
     */
    public static String getProperties(String proName, String params) {
        ResourceBundle resource = ResourceBundle.getBundle(proName);
        String sentUrl = resource.getString(params);
        return sentUrl;
    }

2,通过jdk提供的java.util.Properties类

         

public static void main(String[] args) {
        // 加载属性文件 获取inStream
        InputStream inStream = StringUtils.class.getClassLoader().getResourceAsStream("location.properties");

        Properties prop = new Properties();
        try {
            prop.load(inStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String key = prop.getProperty("urlHeader");
        System.out.println(key);

    }

    加载属性文件的方法有好多种,我就写了一种其中( StringUtils.class 是自己的写的一个工具类),然后得到这个类加载器  getClassloader() ,  getResourceAsStream("XXX.properties") -----文件在src下

原文地址:https://www.cnblogs.com/xinxin-ting/p/8510891.html