zoukankan      html  css  js  c++  java
  • 读取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的值
        }
    }   
  • 相关阅读:
    窗内的星星
    亚特兰蒂斯
    你能回答这些问题吗
    区间最大公约数
    集训队8月14日(树状数组)
    一个简单的整数问题2
    谜一样的牛
    楼兰图腾
    Eternal Victory
    集训队8月12日(并查集)
  • 原文地址:https://www.cnblogs.com/wdpnodecodes/p/7820473.html
Copyright © 2011-2022 走看看