读取Maven项目下resources目录下的配置文件(properties为例)

配置文件:xxxxx.properties

1 a.url=********************
2 b.url=----------------------------------

读取配置文件:


import java.io.InputStream;
import java.net.URL;
import java.util.Iterator;
import java.util.Properties;

public class ReadProperties {
        
    public String getUrlValue(String urlName) {
        String url = null;
        Properties prop = new Properties();
        try {
            ClassLoader classLoader = ReadProperties.class.getClassLoader();// 读取属性文件xxxxx.properties
            InputStream in = classLoader.getResourceAsStream("xxxxx.properties");
            prop.load(in); /// 加载属性列表
            Iterator<String> it = prop.stringPropertyNames().iterator();
            while (it.hasNext()) {
                if (it.next().equals(urlName)) {
                    url = prop.getProperty(urlName);
                }
            }
            in.close();
        } catch (Exception e) {
            
        }
        return url;
    }
    public static void main(String[] args) {
        new ReadProperties().getUrlValue("a.url"); // 获取a.url的值
    }
}   

 

posted @ 2017-11-12 00:20  向前爬的蜗牛  阅读(7389)  评论(0编辑  收藏  举报