Java读取src下config文件夹下的配置文件

config.properties配置文件:

#参数
param=xxx
username=xxx
password=xxxxxxx
getUrl=xxx

-----------------------------------------------------------------

测试代码:

package com.xxx.client;

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

public class ReadProperties {

    public static void main(String[] args) throws IOException {

        Properties properties = new Properties();

        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = HttpRequestUtils.class.getClassLoader().getResourceAsStream("config.properties");
        // 使用properties对象加载输入流

        properties.load(in);

        // 获取key对应的value值
        String property1 = properties.getProperty("username");
        
        String property2 = properties.getProperty("password");
        
        String property3 = properties.getProperty("getUrl");
        
        System.out.println("测试:" + property1);
        System.out.println("测试:" + property2);
        System.out.println("测试:" + property3);

    }

}
 

相关文章: