讀取Maven項目下resources目錄下的配置文件(properties為例)
阿新 • • 發佈:2017-11-12
pro int color -- log getprop tor prop getpath
配置文件: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為例)