读取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的值
    }
}   

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM