讀取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