首页 > 其他 > 详细

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

时间:2017-11-12 00:57:09      阅读:1359      评论:0      收藏:0      [点我收藏+]

配置文件:xxxxx.properties

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

读取配置文件:

public class ReadJdProperties {
        
    public String getUrlValue(String urlName) {
        String url = null;
        Properties prop = new Properties();
        try {
            ClassLoader classLoader = ReadJdProperties.class.getClassLoader();  
            URL resource = classLoader.getResource("xxxxx.properties");  
            String path = resource.getPath();  
            System.out.println(path);  
            // 读取属性文件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 ReadJdProperties().getUrlValue("a.url"); // 获取a.url的值
    }
}   

 

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

原文:http://www.cnblogs.com/wdpnodecodes/p/7820473.html

(0)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!