Here you can find the source of readPropertyFile(URL url)
Parameter | Description |
---|---|
url | is the url to be read. |
Parameter | Description |
---|---|
IOException | is thrown in cases of IO failures. |
public static Properties readPropertyFile(URL url) throws IOException
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Map.Entry; import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { /**/*from ww w . j ava 2s . c om*/ * This class reads a properties file and replaces constructs surrounded by * ${ and } by a system properties specified within the curly brackets. For * example, the construct ${user.home} is replaced by the used home * directory. * * @param url * is the url to be read. * @return Returns a Properties object containing the properties. * @throws IOException * is thrown in cases of IO failures. */ public static Properties readPropertyFile(URL url) throws IOException { if (url == null) { throw new IllegalArgumentException("URL must not be null."); } try (InputStream inputStream = url.openStream()) { Properties properties = new Properties(); properties.load(inputStream); Pattern pattern = Pattern.compile("\\$\\{([\\w.]+)\\}"); for (Entry<Object, Object> entry : properties.entrySet()) { Object key = entry.getKey(); String value = (String) entry.getValue(); while (true) { Matcher matcher = pattern.matcher(value); if (!matcher.find()) { break; } String string = matcher.group(1); String propertyValue = System.getProperty(string); value = value.replaceAll("\\$\\{" + string + "\\}", propertyValue); } properties.put(key, value); } return properties; } } }